Search code examples
androidandroid-fragmentsandroid-preferences

Android - Including a ButtonBar at bottom of SettingsActivity Screen?


I am using the Preferences-API to create my Settings Screen.
As of right now, my SettingsActivity Layout XML contains a simple LinearLayout, with a FrameLayout inside it, which I am using to contain my Preferences fragment.

HOWEVER, due to the way the user accesses the Settings Screen, there is NO 'back' Button in the AppBar - and for this reason, I would like to show a ButtonBar at the bottom of the Preferences screen, so they can click a cancel or ok Button..

Here is what I've attempted so far, to add the ButtonBar:

<?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:orientation="vertical"
    tools:context=".SettingsActivity">

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

<LinearLayout
    style="?android:attr/buttonBarStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button style="?android:attr/buttonBarButtonStyle"
        android:id="@+id/btn_settingsActivity_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@android:string/cancel" />

    <Button style="?android:attr/buttonBarButtonStyle"
        android:id="@+id/btn_settingsActivity_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@android:string/ok" />

</LinearLayout>
</LinearLayout>

Does anybody know what I am doing wrong?
With the code above, the FrameLayout still takes up the entire screen.
If I give the FrameLayout a specific height, I can then see the ButtonBar.
However, I know this is not how I should be doing it.

Can someone please help explain how I should be going about doing this?

Thanks!


Solution

  • First add some orientation in your child LinerLayout e.g. android:orientation="horizontal" and change the height to wrap_content.

    <LinearLayout
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">..
    

    Second tell your FrameLayout that you want to occupy the remaining space via weight property e.g. android:layout_weight="1" and for performance purposes set the height to zero android:layout_height="0dp".

    <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    

    The final code should look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    
        <LinearLayout
            style="?android:attr/buttonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/btn_settingsActivity_cancel"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@android:string/cancel" />
    
            <Button
                android:id="@+id/btn_settingsActivity_ok"
                style="?android:attr/buttonBarButtonStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@android:string/ok" />
    
        </LinearLayout>
    </LinearLayout>