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 (

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=""
    android:orientation="vertical" >

        android:layout_weight="1" >

    <!-- Footer -->
        style="@android:style/ButtonBar" >

            android:text="@string/notification_cancel" />
            android:text="@string/notification_save" />        


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="" >

        android:title="@string/pref_enabled_title" />


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) {

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!



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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s