2

I have two TextView in a LinearLayout, I want to align them one to the left (or center) and one to right in the same line. How to do this? I try to use gravity but they ignore it.

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

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="TextView" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="right"
    android:text="TextView" />

</LinearLayout>
Bob
  • 436
  • 5
  • 13

5 Answers5

2

The easiest way is to change your LinearLayout to a RelativeLayout.

You can use android:layout_alignParentRight="true" and android:layout_alignParentLeft="true". Or to center it use android:layout_centerInParent="true"

See here why gravity won't work

You are using gravity instead of layout_gravity which is what you would want. This post should help clarify the difference

The docs show you available properties.

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
1

android:gravity is used to set the gravity of content inside the view. However, in your case the width is wrap_content, hence the content has nowhere to go in the text views.

Use a RelativeLayout with layout_width as match_parent. Then use the android:layout_alignParentLeft="true" and android:layout_alignParentRight="true"with the textViews.

Shivam Verma
  • 7,973
  • 3
  • 26
  • 34
1

Use it with or without the android:gravity in the second textview and try .

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:gravity="right"
        android:layout_weight="1" />
</LinearLayout>
saruftw
  • 1,104
  • 2
  • 14
  • 37
0

If LinearLayout is Vertical, you can put only an object per line. You can use RelativeLayout, or else put in a line a LinearLayout Horizontal, that contains textviews

ex.

<LinearLayout vertical> <LinearLayout horizontal> <textview 1></> <textview 2></> </LinearLayout> </LinearLayout>

ZanoOnStack
  • 389
  • 2
  • 11
0

I fixed all my issues with GridLayout...is the best thing bcos u don't need to align anithing to nothing...just put what u want into the matrix (row,column)...and this will allow you to visualize all the field in exactly wrap content of your datas also in landscape is perfect!

Alexiscanny
  • 581
  • 7
  • 15