Shared Preferences, Preference Fragment & transparent Navbar

PreferenceActivity is deprecated now but it’s a really nice new solution to manage your preferences. I’m gonna show you a simple implementation of PreferenceFragment, how to use it in Activity which you’d like to make a Preference Page and how to get values in your app. Example is about saving booleans and making preference fields clickable to call a method. Obviously you can put a wide variety of preference fields and it will work out of the box. PreferenceFragment makes everything easier, automated and it’s great.

When I started to learn about creating Android Apps, PreferenceActivity was already deprecated so I had an opportunity to learn something up to date. I really recommend that way of making a preference page. It’s more adjustable and complex solution. First thing to do is to create a preference layout. You can notice it’s located in xml directory while all normal layouts are in layout directory. Well, it’s somehow special and deserves its own location. You don’t have one? Just create the directory in your resources.

I assume you are creating the app using Android Studio. Edit your build.gradle configuration file. While creating this example I used Minimum SDK 14 (Ice Cream Sandwich). Make sure you are using the same value as well to avoid possible errors. PreferenceFragment may not be present in older versions of Android SDK. I’m not sure when it was actually introduced but it’s not so important.

Now a bit of real coding. Create a new class and put the code in there. It will create SettingsFragment which you can use later to display a preference page in an Activity of your choice.

So how to make it useful? How to use it with your Activity? Let’s check! Create a new Activity called SettingsActivity and follow my lead.

Easy, isn’t it? Well, it isn’t working now. We forgot (on puropse) to add strings to strings.xml. You should always save any strings into your strings.xml. It helps creating localizations and editing it in the future.

What’s more important we cannot use SettingsActivity without launching it. We need to create a simple intent to launch it from your app menu. Now a few modifications to your MainActivity which will show you how to get values from shared prefs. When nothing is written (preferences were never changed and are just empty) we define a default behaviour for each one of them. I will also make navigation bar transparent when the specific preference is checked. It’s just an example to show you how to get the values. You can do whatever you want when you know how to do it. Don’t be scared analyzing the code. It seems to be a little complicated but it’s actually not. I added a few things to make it look nice (for example height fix for transparent Navbar) only to show you preference changes the right way. The main goal is to show how to get the values and you can ignore some parts of the code :)

First your main layout.

Now the complete code for MainActivity

Now edit your Manifest and it’s almost done.

Last thing to do – a menu entry to have a possibility to open your Settings.

You are ready to run the app and check it out. What’s important in this example is creating PreferenceFragment and using it in your app to build a Settings Activity. The rest of the code is only to illustrate the example. A really nice extra thing I’ve implemented is a transparent navigation bar with height fix because normally app with transparent Navbar (or transparent Status Bar) is treated like a fullscreen app and the Action Bar overlays the content. Not anymore!

Feel free to reproduce the code in your apps. Here is the complete source code with a live example (apk file) – Preferences.tar.gz

Do you like the post? Leave a comment now!

It's only fair to shareShare on FacebookTweet about this on TwitterShare on Google+Pin on Pinterest