32

I have been trying to use layout_gravity in a horizontal linear layout. But it doesn't seem to work. I don't want to use a relative layout or any other layout. I want to know why it is not working with linear layout.I have earlier used layout_gravity in vertical linear layouts and it has worked in the way I expected.

<LinearLayout
    android:id="@+id/shata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/title_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textColor="@color/black"
        android:text="shata1"
        android:textAppearance="?android:attr/textAppearanceMedium"
        />
    <TextView
        android:id="@+id/map_imageview"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="shata2"/>    
</LinearLayout>

As you can see the the linear layout has a orientation=horizontal and width=match_parent(it is just inside the root layout). But both the TextViews are displayed sticking to the left of the LinearLayouteven though I have given their layout_gravity=center and layout_gravity=right respectively.

Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
Ashwin
  • 12,691
  • 31
  • 118
  • 190
  • It will not work because the Textview's layout is bounded to `wrap_content` not `match_parent` – ImMathan Oct 12 '15 at 08:05
  • Linear layout doesn't work you want. You should use relative layout. Why don't you want to use relative layout? – Muzaffer Oct 12 '15 at 08:12
  • 2
    layout_gravity in a LinearLayout can only change the objects position perpendicular to the orientation. If orientation is horizontal, you can specify top and bottom, not left and right. – Raghu Teja Oct 12 '15 at 08:57
  • @chitti : But I have seen layout_gravity=bottom work in veritcal linear layouts. – Ashwin Oct 12 '15 at 09:09
  • @Ashwin you may want to check that again. You may have seen gravity = bottom for the linear layout work. – Raghu Teja Oct 12 '15 at 10:49

6 Answers6

59

Short answer: If you have a horizontal LinearLayout, then each child can only have layout_gravity values of top, bottom, and center.

The reason for this is because you've already told the LinearLayout to align each child horizontally, left-to-right in the order you specify, which means that it only lets you specify the vertical alignment of each child in the layout_gravity parameter, and the opposite applies when your LinearLayout is vertical.

There's a good link explaining how the various gravities work here.

Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
  • 2
    Even I thought so, at first. But by that logic, vertical LinearLayout should only take values of left, right and center. But vertical layout reacts to the attribute bottom as well. – Ashwin Oct 12 '15 at 09:08
  • 1
    I'd be interested to see an example where vertical responds to `bottom`, because as far as my understanding goes, it shouldn't, and it may just be an artefact of the weights assigned to the other children. – Matt Taylor Oct 12 '15 at 10:03
11

As an addition to Matt Taylor's response, you can specify the size ratio of the elements inside the layout using the layout_weight property. See this thread for more details.

The code above will split the horizontal space in three equals areas.

<LinearLayout
    android:id="@+id/shata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="0"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="0"
        android:layout_height="match_parent"
        android:layout_weight="1"/>    
</LinearLayout>

So in this example

  • the first element looks like layout_gravity="left"
  • the second element looks like layout_gravity="center"
  • the third element looks like layout_gravity="right"
BSMP
  • 4,596
  • 8
  • 33
  • 44
ThomasThiebaud
  • 11,331
  • 6
  • 54
  • 77
  • Thomas, the layout_weight of your bottom TextView is missing the closing double-quote (after the 1). Just fyi. – Alyoshak Apr 05 '17 at 16:31
  • Very Limited practicality. Cuz what if you wanna have two elements on the left side (one after another) and one element on the right side??? – ekashking Oct 05 '20 at 20:32
3

you can divide layout to 3 part then in right part put "shata2" in center part put "shata1" and left part put a transparent view.

<LinearLayout
    android:id="@+id/shata"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        />

    <TextView
        android:id="@+id/title_textview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center|top"
        android:layout_weight="1"
        android:text="shata1"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#111" />

    <TextView
        android:id="@+id/map_imageview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="right"
        android:gravity="right"
        android:text="shata2"
        android:textAppearance="?android:attr/textAppearanceMedium" />



</LinearLayout>

but in your code when you use linear layout it just put views one after other and it doesnt care the layout gravity. hope it be useful .

saleh sereshki
  • 2,402
  • 3
  • 17
  • 18
2

If you want to have your child view centered then you just need to give your linear layout gravity for the children. On your linear layout set the following:

android:gravity="center"

I hope I got your question right.

Thomas R.
  • 7,988
  • 3
  • 30
  • 39
0

One way for creating three childs in horizontal linearlayout would be(tested):

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

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal"
            android:layout_height="wrap_content"/>

            <TextView
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="" />
truespan
  • 189
  • 1
  • 2
  • 11
-2
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
    style="@style/Base.TextAppearance.AppCompat.Headline"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Blankets &amp;  Carpets" />

<ListView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/list_home_feature"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="30dp"
    android:layout_marginRight="10dp">

</ListView>

</LinearLayout>
Android
  • 535
  • 5
  • 16