Position button at the bottom of the layout

Asked

Viewed 1,014 times

1

I would like to position the Exit button at the end of my layout. I am using Linearlayout. Some hint on how to position ?

This is the Linear Layout code:

<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:orientation="vertical"
    tools:context="com.meuprojeto.perfilloginsenha.PerfilActivity">

This is the button code:

<Button
    android:id="@+id/btnLogOut"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:text="SAIR"
    android:textColor="@color/branco"
    android:textSize="20dp"
    android:textStyle="bold" />

layout

1 answer

2


Contrary to popular belief, a Linearlayout, match_parent does not make it occupy all the Parent. The old name, fill_parent, in that sense, it was even worse.

Put a view before the button so that it takes up all the space left of the layout. This will require the button to be positioned at the bottom.

So that a view occupies all remaining available space should it be assigned a layout_weight.

Example:

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

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Space
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Botão"/>
</LinearLayout>

An alternative is to use a Relativelayout and add android:layout_alignParentBottom="true" to the button:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Botão"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>
  • assigns the <Space...> with the property android:layout_weight="1" and nothing happened, but when I changed the layout_height from zero to another value it went down, but I think it would not be ideal, because it would be a gambiarra and on smaller screens the button would hide.

  • The layout of the answer has been tested, so it works. As you have not put your layout complete I don’t know if there’s anything else that needs to be done.

  • I did a test with your code really works, I’ll check on mine what might be different

Browser other questions tagged

You are not signed in. Login or sign up in order to post.