0

I'm trying to put those two buttons in the bottom of my layer. But when I put the gravity of them to "bottom" nothing happens. Please help me. I have tried to put the gravity of my second linearLayout "bottom" too with wrap_content as height but still not working.

[![<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.emmanuilvaresis.oldiefinder.AppSettings"
    android:orientation="vertical"
    android:background="@drawable/background_img">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:textSize="22sp"
        android:text="@string/keyword"
        android:id="@+id/lblKeyword"
        android:textColor="#ffffff" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtKeyword"
        android:textSize="20sp"
        android:textColor="#cacaca" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:textSize="22sp"
        android:text="@string/number"
        android:id="@+id/lblNumber"
        android:textColor="#ffffff" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtNumber"
        android:textSize="20sp"
        android:textColor="#cacaca" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="italic"
        android:textSize="22sp"
        android:text="@string/address"
        android:id="@+id/lblAddress"
        android:textColor="#ffffff" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtAddress"
        android:textSize="20sp"
        android:textColor="#cacaca"
        android:hint="@string/addresshint"
        android:textColorHint="#cacaca" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnOK"
        android:text="@string/btnOK"
            android:layout_gravity="bottom" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btnCancel"
            android:text="@string/btncancel"
            android:layout_gravity="bottom|center" />
    </LinearLayout>

</LinearLayout>][1]][1]
Manos Varesis
  • 145
  • 1
  • 1
  • 6

2 Answers2

0

layout_gravity doesnt work with LinearLayout. You should use FrameLayout for parent of the view which has layout_gravity attributes.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_img"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.emmanuilvaresis.oldiefinder.AppSettings">

<TextView
    android:id="@+id/lblKeyword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/keyword"
    android:textColor="#ffffff"
    android:textSize="22sp"
    android:textStyle="italic" />

<EditText
    android:id="@+id/txtKeyword"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#cacaca"
    android:textSize="20sp" />

<TextView
    android:id="@+id/lblNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/number"
    android:textColor="#ffffff"
    android:textSize="22sp"
    android:textStyle="italic" />

<EditText
    android:id="@+id/txtNumber"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#cacaca"
    android:textSize="20sp" />

<TextView
    android:id="@+id/lblAddress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/address"
    android:textColor="#ffffff"
    android:textSize="22sp"
    android:textStyle="italic" />

<EditText
    android:id="@+id/txtAddress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/addresshint"
    android:textColor="#cacaca"
    android:textColorHint="#cacaca"
    android:textSize="20sp" />

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:orientation="vertical">

        <Button
            android:id="@+id/btnOK"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btnOK" />

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/btncancel" />
    </LinearLayout>
</FrameLayout>
</LinearLayout>
Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
0

You want to place a horizontal linear layout with two buttons at the bottom of screen. You can simply wrap the remaining content in to vertical linear layout and set its layout_weight as 1.

Sample code:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            //REPLACE BELOW CODE WITH YOUR REMAINING CODE WRAPPED INSIDE A LINEAR LAYOUT, APART FROM THOSE BUTTONS.
            <include
                layout="@layout/YOUR_REMAINING_CONTENT"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1.0" />
            //YOUR LINEARLAYOUT WITH BUTTONS WHICH NEEDS TO STAY AT BOTTOM.
            <include layout="@layout/YOUR_BOTTOM_CONTENT"/> 
</LinearLayout>
Ramesh
  • 1,287
  • 1
  • 9
  • 14