Search code examples
androidxmlandroid-linearlayoutcentering

Android layout: center and align to the bottom text view


I would need to align the reset button to the bottom and center it. Here is the layout: Android layout

here is my code:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="Team A" />

        <TextView
            android:id="@+id/team_a_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="0" />

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

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

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

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="Team B" />

        <TextView
            android:id="@+id/team_b_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="0" />

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

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="addOneForTeamB"
            android:text="Free throw" />


    </LinearLayout>

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

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/linear1"
            android:layout_margin="8dp"
            android:onClick="ResetTeamAandB"
            android:text="Reset" />
    </RelativeLayout>

</LinearLayout>

The problem is my parent layout is linear in the horizontal orientation and it adds the button in the third column. That is why I am trying to nest it in the relative layout and align it somehow. Any ideas would be very much appreciated:)


Solution

  • I came up with an alternative solution and it works well. I wrap everything in the relative layout, so my horizontal linear layout is positioned at the top and then the button positioned at the bottom of the relative layout. Here is the xml:

    <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:orientation="horizontal">
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="Team A" />
    
            <TextView
                android:id="@+id/team_a_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="0" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addThreeForTeamA"
                android:text="+3 Points" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addTwoForTeamA"
                android:text="+2 Points" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addOneForTeamA"
                android:text="Free throw" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="Team B" />
    
            <TextView
                android:id="@+id/team_b_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="0" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addThreeForTeamB"
                android:text="+3 Points" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addTwoForTeamB"
                android:text="+2 Points" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addOneForTeamB"
                android:text="Free throw" />
    
        </LinearLayout>
    
    </LinearLayout>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="8dp"
        android:onClick="ResetTeamAandB"
        android:text="Reset" />