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”
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.
Thank you. 3 hours of analysis and finally your blog post solve the same issue.