androidandroid-layoutandroid-linearlayoutgravitylayout-gravity

Linear Layout layout_gravity on View is not working


I am building an application where I need to make some layout. Normally I use Relative Layout and custom views which makes my work however this time, I think of using Liner Layout so I made this below XML. My aim was to make a child linear layout inside a parent and make than child's gravity to "bottom" which will hold my two bottom, one should be right aligned and one should be left aligned.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:gravity="bottom"       
    >       
   <Button            
        android:gravity="left"
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Previous" />
   <Button            
        android:id="@+id/button3"
        android:layout_gravity="right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"/>
</LinearLayout>

The child is sitting in the bottom however two views that is buttons are not aligned properly. Am I doing anything wrong, or can these things not be achieved through this Layout?


Solution

  • Pure LinearLayout used:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="5dp"
        android:paddingRight="5dp"
        android:orientation="vertical"
        android:weightSum="1">
    
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"></LinearLayout>
    
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Previous"
                android:id="@+id/button"
                android:layout_weight="1" />
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Next"
                android:id="@+id/button2"
                android:layout_weight="1" />
        </LinearLayout>
    

    To explain you need to maximize the use of weightSum for this. also the another layout that doesn't contain anything can contain your other widgets just in case you need to add some TextViews, etc. Also this is applied so that the other layout will be forced to be placed in bottom of the whole parent LinearLayout.

    Just in case you wanted to have a space between those two buttons on the middle, you can always make use of the padding. ;)