Search code examples
androidxmlandroid-layoutgravity

layout_gravity="bottom" doesn't work


Hello stackoverflow community! I'm newbie both in programming and on this site, but let's get to the point. I wrote an app:

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:layout_marginTop="10dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical"
        tools:context="com.example.android.courtcounter.MainActivity"
        android:padding="24dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="Team A"
            android:textSize="14sp"
            android:fontFamily="sans-serif-medium"/>

        <TextView
            android:id="@+id/team_a_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="0"
            android:textSize="56sp"
            android:textColor="#000000"
            android:fontFamily="sans-serif-light" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus3a"
            android:text="+3 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus2a"
            android:text="+2 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus1a"
            android:text="Free throw" />
    </LinearLayout>

    <View
        android:layout_width="1dp"
        android:background="@android:color/darker_gray"
        android:layout_height="match_parent">

    </View>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical"
        tools:context="com.example.android.courtcounter.MainActivity"
        android:padding="24dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="Team B"
            android:textSize="14sp"
            android:fontFamily="sans-serif-medium"/>

        <TextView
            android:id="@+id/team_b_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="0"
            android:textSize="56sp"
            android:textColor="#000000"
            android:fontFamily="sans-serif-light"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus3b"
            android:text="+3 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus2b"
            android:text="+2 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus1b"
            android:text="Free throw" />
    </LinearLayout>

</LinearLayout>

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:onClick="scoreReset"
        android:text="reset" />
    </LinearLayout>

</LinearLayout>

I have a problem with positioning a button at the end. I want it to be at the bottom of a screen and centered horizontally. I know I could use relative layout but I decided to check if I can do this using linear layout. And seems that I can't... "bottom" attribute is being ignored.


Solution

  • Simply do one change.

    Remove this line from the last button

    android:layout_gravity="bottom|center_horizontal"
    

    And add this line in the Linear layout which is containing your last button

    android:gravity="bottom|center"
    

    Here is the output:

    enter image description here

    Follow this link to get a clear idea about gravity and layout_gravity. It'll surely help you in your upcoming designs.