anon
anon

Reputation:

Move buttons to bottom half on android

I've been trying to find a way to move my buttons to the bottom half in my xml file. Currently they are in the top half.

enter image description here

I am trying to follow this solution found here, but I am afraid that it is inapplicable.

Put buttons at bottom of screen with LinearLayout?

My buttons.xml is:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/layout"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:weightSum="1">

    <TableLayout android:id="@+id/tableLayout1"
                 android:layout_height="wrap_content"
                 android:layout_width="fill_parent" />

    <Button
        android:id="@+id/block_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:layout_weight="0.09"
        android:background="#ff5ac3ff"
        android:drawableLeft="@drawable/ic_action"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/block_apps"
        android:textSize="22sp" />

    <Button
        android:id="@+id/security_settings_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:layout_weight="0.09"
        android:drawableLeft="@drawable/ic_settings"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/security_settings"
        android:textSize="22sp"/>

    <Button
        android:id="@+id/blacklist_whitelist_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:drawableLeft="@drawable/ic_blacklist_red"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/blacklist_whitelist"
        android:textSize="22sp"
        android:layout_weight="0.09"/>


</LinearLayout>

Upvotes: 1

Views: 179

Answers (3)

hungryghost
hungryghost

Reputation: 9673

You should try something like this. Basically, if you're using layout_weight, you need to set the height or width (depending or your orientation: vertical or horizontal) to "0dp". Otherwise, the view won't use the weight attribute. Try this xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:id="@+id/layout"
                  android:orientation="vertical"
                  android:layout_width="fill_parent"
                  android:layout_height="fill_parent">

        <TextView android:id="@+id/emptySpace"
                  android:layout_height="0dp"
                  android:layout_width="fill_parent"
                  android:layout_weight="3" />

        <Button
            android:id="@+id/block_button"
            style="?android:attr/borderlessButtonStyle"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="right|center_vertical"
            android:layout_weight="1"
            android:background="#ff5ac3ff"
            android:drawableLeft="@drawable/ic_action"
            android:gravity="center_horizontal|center_vertical"
            android:text="@string/block_apps"
            android:textSize="22sp" />

        <Button
            android:id="@+id/security_settings_button"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#ff5ac3ff"
            style="?android:attr/borderlessButtonStyle"
            android:layout_weight="1"
            android:drawableLeft="@drawable/ic_settings"
            android:gravity="center_horizontal|center_vertical"
            android:text="@string/security_settings"
            android:textSize="22sp"/>

        <Button
            android:id="@+id/blacklist_whitelist_button"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#ff5ac3ff"
            style="?android:attr/borderlessButtonStyle"
            android:drawableLeft="@drawable/ic_blacklist_red"
            android:gravity="center_horizontal|center_vertical"
            android:text="@string/blacklist_whitelist"
            android:textSize="22sp"
            android:layout_weight="1"/>
    </LinearLayout>

Btw, I changed your tablelayout to an empty textview, but you should be able to use any other type of view there. It's just there to fill up space.

Also, with layout_weights, be careful with layouts that extend beyond the screen size (like scrollview). That will throw off your weights. Layout weights are best suited for known layout dimensions.

Upvotes: 2

Ryhan
Ryhan

Reputation: 1885

I've tested this, it should work, you'll have to change the button attributes since I was using test content & images.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">

    <TableLayout android:id="@+id/tableLayout1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:gravity="bottom"
    android:orientation="vertical">
    <Button
        android:id="@+id/block_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/block_apps"
        android:textSize="22sp" />

    <Button
        android:id="@+id/security_settings_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center_horizontal|center_vertical"
        android:text="security settings"
        android:textSize="22sp"/>

    <Button
        android:id="@+id/blacklist_whitelist_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:drawableLeft="@drawable/ic_launcher"
        android:gravity="center_horizontal|center_vertical"
        android:text="black white list"
        android:textSize="22sp"/>
</LinearLayout>

</LinearLayout>

Upvotes: 0

Lal
Lal

Reputation: 14810

I would suggest you to divide the whole layout into 6 equal LinearLayouts and then set the buttons in the last three layouts..

Try this code..

<LinearLayout android:orientation="horizontal" android:layout_height="fill_parent" android:layout_width="fill_parent">
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout>
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <Button
        android:id="@+id/block_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:layout_weight="0.09"
        android:background="#ff5ac3ff"
        android:drawableLeft="@drawable/ic_action"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/block_apps"
        android:textSize="22sp" />

  </LinearLayout>
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">

     <Button
        android:id="@+id/security_settings_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:layout_weight="0.09"
        android:drawableLeft="@drawable/ic_settings"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/security_settings"
        android:textSize="22sp"/>

  </LinearLayout>
  <LinearLayout android:layout_weight="1" android:layout_height="fill_parent" android:layout_width="fill_parent">
     <Button
        android:id="@+id/blacklist_whitelist_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff5ac3ff"
        style="?android:attr/borderlessButtonStyle"
        android:drawableLeft="@drawable/ic_blacklist_red"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/blacklist_whitelist"
        android:textSize="22sp"
        android:layout_weight="0.09"/>

   </LinearLayout>
</LinearLayout>

Please do tell me what the output is..

Upvotes: 1

Related Questions