Search code examples
androidtextviewandroid-relativelayout

Android: how do I right-justify a TextView?


I am trying to right-justify the TextView "Due:" on the Subhead line in the CardView image below. I'd like the text to be just to the left of the "11/30/2100" date. Layout code is below. What am I missing here?

enter image description here

layout.xml

...
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"  >

    <CheckBox
        android:id="@+id/chkSelected"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="2dp"
        android:layout_marginLeft="2dp"
        android:layout_centerVertical="true"
        android:clickable="false"
        android:focusable="false"  />

    <TextView
        android:id="@+id/cardBlankText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/chkSelected"
        android:layout_toEndOf="@+id/chkSelected"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"
        android:textColor="@android:color/black"
        android:singleLine="true"
        style="@style/Base.TextAppearance.AppCompat.Headline"  />

    <TextView
        android:id="@+id/cardBlankText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cardBlankText2"
        android:layout_toRightOf="@+id/chkSelected"
        android:layout_toEndOf="@+id/chkSelected"
        android:gravity="start"
        android:layout_marginLeft="4dp"
        android:layout_marginStart="4dp"            
        android:textColor="@android:color/black"
        android:singleLine="true"
        style="@style/Base.TextAppearance.AppCompat.Subhead"  />

    <TextView
        android:id="@+id/cardBlankText4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/cardBlankText3"
        android:layout_toEndOf="@+id/cardBlankText3"
        android:layout_alignParentBottom="true"
        android:gravity="end"
        android:text="Due:"
        android:textColor="@android:color/black"
        android:singleLine="true"
        style="@style/Base.TextAppearance.AppCompat.Subhead"  />

    <TextView
        android:id="@+id/cardBlankText5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/cardBlankText4"
        android:layout_toEndOf="@+id/cardBlankText4"
        android:layout_alignParentBottom="true"
        android:gravity="end"
        android:text="duedate"
        android:textColor="@android:color/black"
        android:singleLine="true"
        style="@style/Base.TextAppearance.AppCompat.Subhead"  />

    </RelativeLayout>

Solution

  • Try with below code.

    Replace in your xml.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <CheckBox
            android:id="@+id/chkSelected"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="2dp"
            android:layout_marginStart="2dp"
            android:clickable="false"
            android:focusable="false" />
    
        <TextView
            android:id="@+id/cardBlankText2"
            style="@style/Base.TextAppearance.AppCompat.Headline"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="4dp"
            android:layout_marginStart="4dp"
            android:layout_toEndOf="@+id/chkSelected"
            android:layout_toRightOf="@+id/chkSelected"
            android:singleLine="true"
            android:textColor="@android:color/black" />
    
        <TextView
            android:id="@+id/cardBlankText3"
            style="@style/Base.TextAppearance.AppCompat.Subhead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/cardBlankText2"
            android:layout_marginLeft="4dp"
            android:layout_marginStart="4dp"
            android:layout_toEndOf="@+id/chkSelected"
            android:layout_toRightOf="@+id/chkSelected"
            android:gravity="start"
            android:singleLine="true"
            android:textColor="@android:color/black" />
    
        <TextView
            android:id="@+id/cardBlankText4"
            style="@style/Base.TextAppearance.AppCompat.Subhead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@+id/cardBlankText5"
            android:gravity="end"
            android:singleLine="true"
            android:text="Due:"
            android:textColor="@android:color/black"
            android:textSize="20dp" />
    
        <TextView
            android:id="@+id/cardBlankText5"
            style="@style/Base.TextAppearance.AppCompat.Subhead"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"
            android:gravity="end"
            android:singleLine="true"
            android:text="duedate"
            android:textColor="@android:color/black"
            android:textSize="20dp" />
    
    </RelativeLayout>
    

    Here is screen shot how it Looks with Aboce XML.