Search code examples
androidxmlandroid-layoutandroid-linearlayout

Evenly space TextViews across screen with right and left justification


I want to space out 3 TextViews across the width of the screen, with the 1st TextView touching the left margin, and the 3rd TextView touching the right margin.

Currently I'm able to achieve the following:

|text[space]text[space]text[space]|

and

|[space]text[space]text[space]text|

and

|[space/2]text[space]text[space]text[space/2]|

By setting left, right, and center gravities, respectively, within a horizontal ListView.

I want to achieve:

|text[space]text[space]text|

But I can't seem to get it. Here's my XML:

<LinearLayout
    android:id="@+id/graph_xaxis_labels"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/linegraph_fragment"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:orientation="horizontal">

    <TextView
    android:id="@+id/textview_xaxis_0"
    android:layout_width="0px"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:textSize="8sp"
        android:text="02/12"
        />
    <TextView
        android:id="@+id/textview_xaxis_1"
        android:layout_width="0px"
        android:layout_height="wrap_content" 
        android:layout_weight="1"

        android:textSize="8sp"
        android:text="02/13"
        />
    <TextView
        android:id="@+id/textview_xaxis_2"
        android:layout_width="0px"
        android:layout_height="wrap_content" 
        android:layout_weight="1"

        android:textSize="8sp"
        android:text="02/14"
        />


</LinearLayout>

Any help will be greatly appreciated.

PS: I know hardcoding TextView text is bad. That's only temporary.


Solution

  • You're almost there. You're missing the Gravity.

    UPDATE: To center more than three, you need to change the layout… updated below for 6 labels: (The hint was given by Haresh answer) It has a few drawbacks but should do the trick.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/graph_xaxis_labels"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:gravity="center"
                  android:orientation="horizontal">
      <TextView
          android:id="@+id/textview_xaxis_0"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="8sp"
          android:text="02/12"/>
      <View
          android:layout_width="0dp"
          android:layout_height="1dp"
          android:layout_weight="1"/>
      <TextView
          android:id="@+id/textview_xaxis_1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="8sp"
          android:text="02/13"/>
      <View
          android:layout_width="0dp"
          android:layout_height="1dp"
          android:layout_weight="1"/>
      <TextView
          android:id="@+id/textview_xaxis_2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="8sp"
          android:text="02/14"/>
      <View
          android:layout_width="0dp"
          android:layout_height="1dp"
          android:layout_weight="1"/>
      <TextView
          android:id="@+id/textview_xaxis_3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="8sp"
          android:text="02/15"/>
      <View
          android:layout_width="0dp"
          android:layout_height="1dp"
          android:layout_weight="1"/>
      <TextView
          android:id="@+id/textview_xaxis_4"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="8sp"
          android:text="02/16"/>
      <View
          android:layout_width="0dp"
          android:layout_height="1dp"
          android:layout_weight="1"/>
      <TextView
          android:id="@+id/textview_xaxis_5"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:textSize="8sp"
          android:text="02/17"/>
    </LinearLayout>