-1

I am trying to align a button to bottom of screen but it is not moving. The last button, with id=button 1, is the button in question. I want this button to align right at bottom of screen, but looks like, some combination of the attributes of nested arrays no letting it happen.

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.crackit.crackit.startQuiz1">

<LinearLayout
android:layout_width="wrap_content"
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_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:weightSum="2">
<Button
android:id="@+id/radio0"
android:layout_weight="1"
android:layout_height="140dp"
android:layout_width="140dp"
/>

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

</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_weight="1"
android:layout_height="140dp"
android:layout_width="140dp"
/>

<Button
android:id="@+id/radio3"
android:text="Android"
android:layout_weight="1"
android:layout_height="140dp"
android:layout_width="140dp"
/>
</LinearLayout>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#0000FF"
android:onClick="NextQuestion"
android:layout_gravity="bottom"
android:textColor="#FFFFFF"
android:text="Next" />
</LinearLayout>
</LinearLayout>

I am trying to align a button to bottom of screen but it is not moving. The last button, with id=button 1, is the button in question. I want this button to align right at bottom of screen, but looks like, some combination of the attributes of nested arrays no letting it happen.

Jyoti Gulati
  • 35
  • 1
  • 5

3 Answers3

2

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>
Saurabh Vadhva
  • 511
  • 5
  • 12
1

You can make use of RelativeLayout and then align the Button relative to it. Below is the example for your case.

<?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"
android:layout_gravity="center"
tools:context="com.crackit.crackit.startQuiz1">

<LinearLayout
    android:layout_width="wrap_content"
    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_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center_horizontal"
        android:weightSum="2">
        <Button
            android:id="@+id/radio0"
            android:layout_weight="1"
            android:layout_height="140dp"
            android:layout_width="140dp"
            />

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

    </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_weight="1"
            android:layout_height="140dp"
            android:layout_width="140dp"
            />

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

</LinearLayout>
<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#0000FF"
    android:onClick="NextQuestion"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:textColor="#FFFFFF"
    android:text="Next" />
</RelativeLayout>
Sanjeet
  • 2,385
  • 1
  • 13
  • 22
0

If you want the button to align to the right, change its width attribute to

android:layout_width="wrap_content"

And then add an attribute 'gravity' to your second linear layout: like this -

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

And if you must use linear layouts, include another empty linear layout before the button, with weight as 1 to use up the remaining space. ( So that button will be at the bottom) Like this-

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
rS_
  • 131
  • 1
  • 1
  • 7