Sunlight 3.06 – The Preference App Bar Dilemma

Using the latest tools (Android Studio and SDK), the AppBar won’t show up in the SettingsActivity. Would be a simple solution to use a PreferenceFragment inside a standard activity with a toolbar – but there are version issues…

[*Caution: Outdated content. My sunlight project has been discontinued. It refers to earlier versions of the v7 appcompat Library (21.0.3) and Android Studio (1.1).  I’ll keep the posts online for reference but with the current version of appcompat Library things may be different / easier today, so please refer to more up-to-date sources.]

This article is part of the Sunlight project and refers to Lesson 3  (Create Settings Activity) of Googles Udacity Course “Developing Android Apps”.

Trouble is, that the SettingsFragment was introduced in Android Version 3 (API 11) but we want to support down to API 10 (Version 2.3.3) which still makes up an amount of > 7% (see stats).

This is a known problem, that is discussed and thoroughly answered in this stack overflow post.

I’d like to mention one more thing that won’t work out: Using the Android Studio wizard to create an new SettingsActivity. This (tested on Android Studio 1.1 RC1) will also create a SettingsActivity the extends PreferenceActivity. With a missing ActionBar due to our Theme.AppCompat.NoActionBar default Style – which will instantly lead to problems if you run the code.

Here are some possible solutions:

The Udacity way

In the course video (starting 0:37), they added a small section, mentioning the problem of the missing action bar and refer to Lesson 5, where the styles of the app will be changed and the AppBar will re-appear. And it will do. But as we try to stick to the new Material Design this would not be the optimum. (Sadly, there is no optimum here…)

Implementing both ways

There are solutions and even libraries to come across this issue. The best place to start reading is the stack overflow post I already mentioned. You will find references to implementation examples and libraries inside the answers.

A simple one

Here is a simple, yet somehow incomplete solution:

Add a specific style to the AndroidManifest.xml

This will lead to a warning in the IDE

but nevertheless this result will show up in the devices. So we choose to ignore it.

And no – it’s not possible to access an AppCompat Style here. So the AppBar in KitKat (and below) will have the old school style.  So some customizing will have to be done to achieve a consistent look. We’ll take care of this in a later post.

In fact, this solution is not much different from the Udacity way. Just kind of anticipating.

Future perspective

The market share of the pre 4.x devices is constantly decreasing. Depending of target market and target audience, the choice for minSdkVersion will often be >= 14. In that case you won’t have to bother and simply go with the PreferenceFragment approach inside a standard Activity with a Toolbar.

Leave a Reply

Your email address will not be published. Required fields are marked *