Search code examples
androidandroid-layoutandroid-linearlayoutandroid-relativelayout

aligning button to bottom within linearlayout parent with minheight


I have a LinearLayout parent which contains two LinearLayout child of which second child contains Button which should be place at bottom of the parent view. I'm using android:layout_gravity="center_horizontal|bottom" on second child LinearLayout

Still I'm getting the output as shown below:Screenshot

I tried using RelativeLayout as a parent but that is making dialog height as big as device screen size which I don't want.

If I remove minHeight attribute from LinearLayout parent, then parent wraps to height less than minHeight

Any hep appreciated.

Here's my code for reference:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background_dialog"
    android:minHeight="200dp"
    android:minWidth="400dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:orientation="vertical" >

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

            <ImageView
                android:id="@+id/ivDialogImage"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="15dp"
                android:src="@drawable/icon_cfs_activated" />

            <TextView
                android:id="@+id/tvDialogMessage"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginLeft="15dp"
                android:gravity="center_vertical"
                android:minHeight="80dp"
                android:paddingRight="5dp"
                android:textColor="@color/text_color"
                android:textSize="15sp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp" >

        <Button
            android:id="@+id/btndialogOk"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/background_button_selector"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="@string/alert_ok"
            android:textColor="@color/text_color" />
    </LinearLayout>

</LinearLayout>

Solution

  • android:layout_gravity defines the gravity for the Layout itself inside it's parent view.

    What you should use to apply gravity for child layouts is:

    android:gravity="center_horizontal|bottom"
    

    Also, your bottom LinearLayout height set to wrap_content the button will fit it not matter what you did with the gravity. add a background color to the LinearLayout to observe that.

    I think what you are looking for, for your bottom layout is:

    <LinearLayout
            android:id="@+id/ll_button"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:layout_gravity="center_horizontal|bottom"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp" >
    

    This will force your bottom layout to fill the rest of the Parent LinearLayout