How to leave a fixed footer in the Android layout?

Asked

Viewed 1,993 times

-1

I have the following layout on xml on my Android designer:

Designer do Layout de Configurações

<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ativar Notificações:" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Inativo"
                android:id="@+id/checkAtivarNotificacao" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tempo em Minutos das Notificações:"
                android:layout_marginTop="10dp" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="none|number"
                android:ems="10"
                android:id="@+id/txtIntervaloNotificacao"
                android:maxLength="4"
                android:focusable="true"
                android:focusableInTouchMode="true"/>
        </LinearLayout>

    </ScrollView>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom|center_horizontal">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Salvar"
        android:id="@+id/btSalvarConfiguracao" />

</LinearLayout>

The problem is that according to what I keep adding components on the screen and Scrollview goes down the save button disappears, so I want to leave a footer space with the save button fixed and the content of the flexible layout regardless of how much increase, exactly as it happens in the standard android messaging app illustrated below:

Layout Android com Rodapé Fixo

1 answer

0


It would be interesting you put your xml, to identify where you went wrong. But since the question is a general one, here is my answer:

Utilize RelativeLayout. Whatever you wish to center within it you use some property of it centralization:

  1. centerInParent - Center in the middle (horizontally and vertically) of RelativeLayout.
  2. centerHorizontal - Centralizes horizontally, in relation to the RelativeLayout
  3. centerVertical - Center vertically, in relation to the RelativeLayout

Code example:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <Button
        android:id="@+id/btn"
        android:layout_height="wrap_content"
        android:layout_width="100dp"
        android:layout_centerInParent="true"/>

</RelativeLayout>

// EDIT

With its best specification in the question, I repeat, assemble a layout of android each has its way, and have several ways to do the same.

In your case I would simply put all this code inside a LinearLayout and assign the property layout_weight=1 in the ScrollView.

Follows code:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

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

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Ativar Notificações:" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Inativo"
                android:id="@+id/checkAtivarNotificacao" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Tempo em Minutos das Notificações:"
                android:layout_marginTop="10dp" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="none|number"
                android:ems="10"
                android:id="@+id/txtIntervaloNotificacao"
                android:maxLength="4"
                android:focusable="true"
                android:focusableInTouchMode="true"/>
        </LinearLayout>

    </ScrollView>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Salvar"
            android:id="@+id/btSalvarConfiguracao"/>
    </LinearLayout>
</LinearLayout>
  • Hugo Fagundes I reformulated the question, maybe now it gets clearer, thank you!

  • Okay, I edited my answer, take a look ;)

Browser other questions tagged

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