James Montemagno
James Montemagno

Live, Love, Bike, and Code.



Lollipop AppCompat PreferenceActivity

James MontemagnoJames Montemagno

I fell in love with material design the moment I saw it and at Evolve 2014 my good friend and co-worker Jérémie Laval and I actually gave 2 sessions on the concept and implementation of material design in your apps. With the introduction of v7 app compat library rev 21 you are now able to upgrade your apps while maintain compatibility with older versions of Android. If you are looking to get started integrating the support library into your apps then check out my samples over on the Xamarin documentation site. While transitioning my Bike Now app over to v7 appcompat I ran into one small issue, which is that there is no updated PreferenceActivity with a SupportActionBar or one that takes advantage of the new Toolbar and back arrow. However, there is an awesome workaround that will allow you to get that beautiful material design in your preference activity.

Special Themes

You should already have a base theme of your application that you are using that inherits from Theme.AppCompat of some sort that is used throughout your application, but you will need to create a new theme specific for the PreferenceActivity.

This will ensure that your toolbar is properly themed with the up indicator and will use your primary colors correctly as well, while still inheriting from your base theme.

Settings Layout

Normally with a preference activity there is no layout file at all because everything is generated for you through the preference xml file. However, we will actually need to insert a new v7 Toolbar at the top of the preference activity. I have a universal Toolbar that I use throughout my application that I specify in my Toolbar.axml and then I created a new Settings.axml which simply stacks the toolbar ontop of a FrameLayout that we will use later on to insert the main preference ListView.

Update PreferenceActivity

The last piece is to now trick our PreferenceActivity into using our new Settings.axml. This is done in the SetContentView where we will combine the two layouts together cleverly.

With that in place you are all set to now have a beautiful preference activity with the back arrow properly in place. In addition to this since you have a full Toolbar you can still use menu items and handle them appropriately as well.

Live, Love, Bike, and Code