Hector Badenes
Hector Badenes

Reputation: 13

Android button permanently on bottom

I have the following Xml code where I want to display the content of a fragment into FrameLayount but I don't know how to keep my button always on bottom.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_lista_preguntas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ingenierovagabundo.encuestafacil.GUI.ListaPreguntasActivity">

<!-- Toolbar -->
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/appbar_nueva_pregunta"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<FrameLayout
    android:id="@+id/contenedor_Pregunta"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</FrameLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="16dp"
    android:text="@string/npa_button_guardar"/>

Upvotes: 0

Views: 1057

Answers (4)

Anirudha Mhase
Anirudha Mhase

Reputation: 91

You could add android:layout_weight="1" to your FrameLayout. This will expand the FrameLayout to fill any remaining space in the parent view. Which should result the Button to be at the bottom. So your resulting FrameLayout will be,

<FrameLayout
    android:id="@+id/contenedor_Pregunta"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1">

</FrameLayout>

For more on layout_weight follow the link https://developer.android.com/guide/topics/ui/layout/linear.html#Weight

Upvotes: 0

Payam Kokabi
Payam Kokabi

Reputation: 222

you can use RelativeLayout insted of LinearLayout as below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_lista_preguntas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ingenierovagabundo.encuestafacil.GUI.ListaPreguntasActivity">

<!-- Toolbar -->
<android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/appbar_nueva_pregunta"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

<FrameLayout
    android:id="@+id/contenedor_Pregunta"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/btn"
    android:layout_below="@+id/appbar_nueva_pregunta">

</FrameLayout>

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="false"
    android:layout_marginBottom="16dp"
    android:layout_marginRight="16dp"
    android:text="@string/npa_button_guardar" />

Upvotes: 1

Aman Shekhar
Aman Shekhar

Reputation: 2780

Use this Code in activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>


        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_main" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:backgroundTint="@color/colorOrange"
            app:srcCompat="@android:drawable/ic_menu_share"
            tools:ignore="VectorDrawableCompat" />

    </android.support.design.widget.CoordinatorLayout>

And in content_main.xml, use this Code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

Upvotes: 0

J-Jamet
J-Jamet

Reputation: 857

You use a LinearLayout as container, then the elements follow each other.

If you want to keep your button always on bottom, use a RelativeLayout with android:layout_alignParentBottom="true".

You also need to close your container! ;)

Upvotes: 0

Related Questions