Question

I wish to display the 2 text views at each end of the screen, I tried using layout_gravity but the are still positioned beside each other.

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_gravity="left"
            android:id="@+id/gameTimer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Time" />

        <TextView
            android:layout_gravity="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/gameScore" 
            android:text="Score"/>

    </LinearLayout>   
Was it helpful?

Solution

Give weight to both textViews

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:gravity="left"
            android:id="@+id/gameTimer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_weight="1"
            android:text="Time" />

        <TextView
            android:gravity="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/gameScore" 
            android:layout_weight="1"
            android:text="Score"/>

    </LinearLayout>   

OTHER TIPS

// try this way,hope this will help you...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/gameTimer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Time" />

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gameScore"
        android:text="Score"/>

</LinearLayout>
<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <TextView

            android:id="@+id/gameTimer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:text="Time" />

        <TextView

            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content"
            android:id="@+id/gameScore" 
            android:text="Score"/>

    </RelativeLayout> 

try this code

Use a relative layout instead of linear layout. You can try gravity or weights or other options then.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top