View only preferences for Android

Have you ever wanted to show a bunch of statistics, or read only config or something like that, and wished you could just use the built in PreferencesActivity like you can for user editable preferences?

Just like how the stock android does for the “About Phone” page and the “SD card & phone storage” Android "preferences" that are uneditable

I wanted to have some figures like this in my own app, and I wanted them to show up under menu → preferences as well. Here’s how…

First, just declare a plain “Preference” in the layout, not an EditTextPreference or anything fancy, we’re going to make a pretty simple custom preference.

<PreferenceScreen xmlns:a="http://schemas.android.com/apk/res/android" a:key="PintyPrefs">
    <PreferenceCategory>
<!-- other stuff -->
    </PreferenceCategory>
    <PreferenceCategory a:title="stats">
        <Preference a:title="@string/preference_last_data_update"              
                            a:key="preference_lookup_key"
                            a:editable="false"
                            a:defaultValue="unknown"
                />
    </PreferenceCategory>
</PreferenceScreen>

Now, in your preferences activity, just stick in some code like this…

//.. onCreate/onResume..
        lastUpdatedPref = findPreference(preference_lookup_key);
        String lastUpdated = calculateYourStatisticsSomehow();
        lastUpdatedPref.setSummary(lastUpdated);

That’s it. You can see here how I’ve got it in my own preferences dialog

In my case I wanted to see when the supporting server for my app had last been updated.

  1. Thank you. 3 hours of analysis and finally your blog post solve the same issue.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>