3

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.

Mido
  • 33
  • 1
  • 1
  • 4

5 Answers5

5

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.

Community
  • 1
  • 1
Sudip Podder
  • 830
  • 11
  • 25
1

You can add this View before last button:

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

    <!-- Add this -->
    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:onClick="scoreReset"
        android:text="reset" />
</LinearLayout>
Hobbajt
  • 267
  • 3
  • 14
1

Try encapsulating the main layout in one LinearLayout with layout_weight = 1, layout_width = "match_parent", layout_height = "match_parent"and button at bottom in another LinearLayout with layout_width="match_parent" and gravity set to bottom|center_horizontal.

Iram Bukhari
  • 487
  • 1
  • 5
  • 15
1

This should work :

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:gravity="bottom|center">

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

</LinearLayout>
HelloSadness
  • 945
  • 7
  • 17
0

use gravity="bottom" instead of layout_gravity it should help.

Sardar Khan
  • 845
  • 6
  • 16