Android mixing preferences with view widgets

Hello Android devs and fans!

It’s been a long time since my last post, cause I had to work hard on a project, but here I am again willing to share a lot of the technical info I gathered during all this period!

Some time ago, during the development of the settings for an Android app, I wanted to mix a preferences screen with another screen that only contained a list view. This task got quite complex, as the use of Fragments API seemed the only option, to me! As you might already know, the Fragments API can be used for prior to Honeycomb Android version, using the compatibility library! Unfortunately, I quickly started changing my activity containing the list view into a fragment, which I was going to embed in the new screen. Only later that I started thinking about the changes required for changing a PreferenceActivity into a fragment, I noticed the the PreferenceFragment class was not included in the support library, while this exists in the Android API for honeycomb Android and later. So, this really disappointed me and made me mad, based on that they intended to provide the full Fragments API for older Android version, but finally decided to leave out a little thing that can cause a lot of trouble! This became a barrier and made be revert back to the previous state.

After a while, it became a requirement of my project for a preferences screen to have some buttons at the bottom of the screen for canceling or saving the changes persistently! This was proved similar to the requirement described before, just I was a bit more lucky this time, or I thought of it in a different way, which probably helped me find a blog post about this (http://baroqueworksdev.blogspot.gr/2012/03/create-preferenceactivity-with-header.html)

So, I am summing up everything here:

1. Create a layout file that should include a list view (with attribute android:id=”@+id/android:list”) and any other view widget you need your screen to have. (main.xml)

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

    <ListView
        android:id="@+id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="0.0dp"
        android:layout_weight="1" >
    </ListView>

    <!-- Footer -->
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        style="@android:style/ButtonBar" >

        <Button
            android:id="@+id/cancelBtn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"     
            android:layout_weight="1"
            android:text="@string/notification_cancel" />
        
        <Button
            android:id="@+id/saveBtn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/notification_save" />        
    </LinearLayout>

</LinearLayout>

2. Create a preferences xml file, from which any preferences will be attached to the screen. (preferences.xml)

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <CheckBoxPreference
        android:key="@string/pref_enabled_key"
        android:summaryOff="@string/pref_enabled_summaryoff"
        android:summaryOn="@string/pref_enabled_summaryon"
        android:title="@string/pref_enabled_title" />

</PreferenceScreen>

3. You can now attach both the layout (containing the view widgets) and the preferences (that will replace the list view from the layout file).

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    addPreferencesFromResource(R.xml.preferences);
}

This is the way that I managed to put a static set of buttons below the preferences screen! Hope this will prove useful to you as well!

Enjoy!

Advertisements

About sermojohn

Professional Software Engineer
This entry was posted in Android and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s