3

I'm trying to have two TextViews side-by-side, and I want one to be touching the right-side of the screen and the other, the left-side. I don't want to define the widths using numbers because screens of different sizes would behave differently. So I'm trying to use layout_gravity, which is not working for some reason.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16dp"
    android:layout_gravity="left"
    android:text="rrr"
    android:textColor="@color/secondTextColor"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:textSize="16dp"
    android:text="sss"
    android:textColor="@color/secondTextColor" />

</LinearLayout>

Can anyone tell me why? Thanks!

João Marcos
  • 75
  • 1
  • 6
  • Are the 2 textviews supposed to touch each other in between? Or 1 only touches the left, the other right and they don't touch in between? If they touch in between then `weight` is enough and you don't need `weightSum` – Ankit Aggarwal Jan 27 '16 at 14:05

3 Answers3

4

You can create one LinearLayout for each TextView as follows :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="start">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:text="rrr"
        android:textColor="#f2f2"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:text="sss"
            android:textColor="#f3f3" />
    </LinearLayout>
</LinearLayout>

And the important thing is that in your first LinearLayout you put android:gravity="start" and in your second one android:gravity="end", then it will work :)

Use end instead of right to ensure correct behavior in right-to-left locales.

Why is "end" better than "right"?

Using Gravity#LEFT and Gravity#RIGHT can lead to problems when a layout is rendered in locales where text flows from right to left. Use Gravity#START and Gravity#END instead. Similarly, in XML gravity and layout_gravity attributes, use start rather than left. For XML attributes such as paddingLeft and layout_marginLeft, use paddingStart and layout_marginStart. NOTE: If your minSdkVersion is less than 17, you should add both the older left/right attributes as well as the new start/right attributes. On older platforms, where RTL is not supported and the start/right attributes are unknown and therefore ignored, you need the older left/right attributes. There is a separate lint check which catches that type of error.

(Note: For Gravity#LEFT and Gravity#START, you can use these constants even when targeting older platforms, because the start bitmask is a superset of the left bitmask. Therefore, you can use gravity="start" rather than gravity="left|start".)

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
  • 1
    Thanks! It worked without 2 more Linear Layouts though. Why is "end" better than "right"? – João Marcos Jan 27 '16 at 14:00
  • 1
    @JoãoMarcos the reason why is better, is basically to easily support RTL languages (Right to left) without changing your layouts, see: https://developer.android.com/training/basics/supporting-devices/languages – moxi Aug 11 '20 at 20:51
3

You can try with android:layout_weight & android:gravity .

Read What does android:layout_weight mean & Layout Weight

 <LinearLayout 
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="horizontal"
   android:weightSum="1" >

 <TextView

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="left"
    android:text="Intellij" />

 <TextView

    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:gravity="right" // You can add end instead of right
    android:text="Amiya" />

 </LinearLayout>
Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

You could use android:layout_weight="1" on TextView's and 0dp for width.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
David Rauca
  • 1,583
  • 1
  • 14
  • 17
  • Why do you consider that it is not elegant? Here is a tutorial with good explanations of how weights can be used in layout files: [link](https://www.youtube.com/watch?v=rMksRBvYG28) – David Rauca Jan 27 '16 at 14:07