Put Buttons at Bottom of Screen with Linearlayout

Put buttons at bottom of screen with LinearLayout?

You need to ensure four things:

  • Your outside LinearLayout has layout_height="match_parent"
  • Your inside LinearLayout has layout_weight="1" and layout_height="0dp"
  • Your TextView has layout_weight="0"
  • You've set the gravity properly on your inner LinearLayout: android:gravity="center|bottom"

Notice that fill_parent does not mean "take up all available space". However, if you use layout_height="0dp" with layout_weight="1", then a view will take up all available space (Can't get proper layout with "fill_parent").

Here is some code I quickly wrote up that uses two LinearLayouts in a similar fashion to your code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/db1_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/cow"
android:layout_weight="0"
android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="1" />

<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="2" />

<Button
android:id="@+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="145dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:text="3" />
</LinearLayout>

</LinearLayout>

The result looks like similar to this:

Sample Image

Align Button to Bottom using LinearLayout

Change second linear layout to

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >

This will put the button at the bottom and this layout will take the rest of the space

Align Buttons in Linear Layout to the Bottom of the Screen

try this :

<LinearLayout
android:id="@+id/llBottomContainer2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="bottom"
android:gravity="bottom"
android:padding="5dp" >

how to align button at the bottom of the screen in LinearLayout

Just reduce the textsize of the button Text. Since it is not able to display text in one line the margin is getting distorted.

Here I have made textSize to 11dp for each buttontext and the view is perfect for NexusOne size display

<?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="match_parent"
android:orientation="vertical" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/parentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>

<LinearLayout
android:id="@+id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="horizontal" >

<Button
android:id="@+id/save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save"
android:textSize="11dp" />

<Button
android:id="@+id/submit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Submit"
android:textSize="11dp" />

<Button
android:id="@+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel"
android:textSize="11dp" />

<Button
android:id="@+id/delete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Delete"
android:textSize="11dp" />

<Button
android:id="@+id/reset"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Reset"
android:textSize="11dp" />
</LinearLayout>

</LinearLayout>

How to fix the button at the bottom on my Linear Layout: Android Studio

Try to use

android:baselineAligned="false"

In your LinearLayout. Let me know if it works for you. That's how I solved a similar issue, but it didn't include an editText

how to align a set of button to bottom of the screen in android

Make the Parent of the LinearLayout containing the button as relative and set the property of linear layout containing two buttons as android:layout_alignParentBottom="true". You can also set the gravity of the Linear Layout to Bottom.

Try this:


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

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- heading for basic settings -->
<TextView
android:id="@+id/setup_macroheading"
style="@style/txtHeading" />

<View
android:layout_width="fill_parent"
android:layout_height="20dp"/>

<!-- macro name -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_weight="1"
android:textColor="@color/white"
android:text="Macro Name"/>

<EditText
android:id="@+id/setup_macroname"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:maxLength="12"
android:inputType="textCapCharacters"
android:singleLine="true"/>
</LinearLayout>

<View
android:layout_width="fill_parent"
android:layout_height="20dp"/>

<ListView
android:id="@+id/lvMacroList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<!-- Save & Cancel button -->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="horizontal"
android:paddingLeft="20dp"
android:paddingRight="20dp" >

<Button
android:id="@+id/setup_macroSavebtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:onClick="onSaveButtonClick"
android:text="Save"/>

<Button
android:id="@+id/setup_macroCancelbtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:onClick="onCancelButtonClick"
android:text="Cancel"/>
</LinearLayout>
</RelativeLayout>

How to align button to bottom of screen in Nested Linear Layouts

Use this code to resolve your problem

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

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

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
android:orientation="horizontal"
android:weightSum="2">

<Button
android:id="@+id/radio0"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_weight="1" />

<Button
android:id="@+id/radio1"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_weight="1" />

</LinearLayout>

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

<Button
android:id="@+id/radio2"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_weight="1" />

<Button
android:id="@+id/radio3"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_weight="1"
android:text="Android" />
</LinearLayout>

</LinearLayout>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_gravity="bottom"
android:background="#0000FF"
android:onClick="NextQuestion"
android:text="Next"
android:textColor="#FFFFFF" />
</RelativeLayout>

Align buttons to the bottom of the layout

This is the property you need to use:

android:layout_alignParentBottom="true"

Try using the following XML and customize your button appearance as you like:

<RelativeLayout 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="#d1000000"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

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

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="PREV"
android:layout_marginRight="-5dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="NEXT"
android:layout_marginLeft="-5dp"/>

</LinearLayout>
</RelativeLayout>


Related Topics



Leave a reply



Submit