Gautier Rio
Gautier Rio

Reputation: 21

Android Studio - Align buttons with different sizes to bottom

I would like to align my buttons on the bottom of my linearlayout (on the same level). I use android:layout_gravity="bottom" but it doesn't work...

    <LinearLayout 
        android:id="@+id/linearalyout1"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_below="@+id/gridView"
        >

        <Button 
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="100dp"
            android:text="Button 1"
            android:layout_gravity="bottom"/>

        <Button 
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="40dp"
            android:text="Button 2"
            android:layout_gravity="bottom"/>

        <Button 
            android:layout_width="0dip"
            android:layout_weight="1"
            android:layout_height="50dp"
            android:text="Button 3"
            android:layout_gravity="bottom"/>
    </LinearLayout>

Here is the output : image Thanks for the help !

Upvotes: 2

Views: 119

Answers (2)

Zain
Zain

Reputation: 40908

LinearLayout has limited features, and you can do this by adjusting the three buttons bottom, start & end constraints of ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_below="@+id/gridView">

    <Button
        android:id="@+id/button1"
        android:layout_width="0dip"
        android:layout_height="100dp"
        android:text="Button 1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dip"
        android:layout_height="40dp"
        android:text="Button 2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintStart_toEndOf="@+id/button1" />

    <Button
        android:id="@+id/button3"
        android:layout_width="0dip"
        android:layout_height="50dp"
        android:text="Button 3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button2" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

Yunus Karakaya
Yunus Karakaya

Reputation: 531

I tried too much but I can not resolve this problem normally. I some changed your base code.

I added vertical Linear Layout and set android:gravity="bottom" for main LinearLayout then problem solved. Maybe it is a bug maybe I do not know solution properly.

<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:gravity="bottom"
tools:context=".MainActivity">


<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        android:text="Button 1" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_weight="1"
        android:text="Button 2" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_weight="1"
        android:text="Button 3" />
</LinearLayout>

Upvotes: 1

Related Questions