Search code examples
androidandroid-layoutandroid-relativelayout

Displaying text in left most position in Relative Layout


'My Topic' text is displayed in centre at the top. And below that there is a list.
I want to display a '<Back' text at the left most position on the same line as 'My Topic'. How can i achieve this?

Here is the relevant layout XML:

 <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="0dp"
            android:background="#FFFFFF"
            android:orientation="vertical"
            android:paddingLeft="0dp" >

            <TextView
                android:id="@+id/topic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:gravity="center_horizontal"
                android:text="My Topic"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold"
                android:fontFamily="sans-serif" />

            <ListView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/topic"
                android:layout_margin="0dp"
                android:paddingLeft="0dp" 
                android:divider="#A2B0B0"
                android:dividerHeight="0.5dp"               
                android:choiceMode="singleChoice" >
            </ListView>
        </RelativeLayout>

Solution

  • replace your layout with this

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="0dp"
        android:background="#FFFFFF"
        android:orientation="vertical"
        android:paddingLeft="0dp" >
    
        <RelativeLayout android:id="@+id/relTopic"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/topic"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_centerHorizontal="true"
            android:text="My Topic"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold"
            android:fontFamily="sans-serif" />
    
            <TextView
                android:id="@+id/back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_alignParentLeft="true"
                android:text="Back"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold"
                android:fontFamily="sans-serif" />
        </RelativeLayout>
    
        <ListView
            android:id="@+id/list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/relTopic"
            android:layout_margin="0dp"
            android:paddingLeft="0dp"
            android:divider="#A2B0B0"
            android:dividerHeight="0.5dp"
            android:choiceMode="singleChoice" >
        </ListView>
    </RelativeLayout>
    

    You can take both textview in single layout and then give its reference to listview android:layout_below="@id/relTopic"