Add ActionBar to a PreferenceActivity
If you are using an AppCompat theme, you have to pay attention to some points.
The PreferenceActivity
doesn't extend the AppCompatActivity
or the deprecated ActionBarActivity
.
As solution you can create a PreferenceFragment
as you are doing and use it in a standard AppCompatActivity
. Of course you can use also a Toolbar
.
Moreover with the new 22.1+ appcompat you can use the AppCompatDelegate to extend AppCompat's support to any Activity.
You can check this official link to AppCompatPreferenceActivity, where you can find an example of this technique.
By explicitly themeing SettingActivity with a theme derived from DarkActionBar, we are able to add back the Action Bar.
I did the same to have an action bar in my Settings activity and it worked for me .
1.First add a different style for your Settings Activity in styles.xml
<style name="SettingsTheme" parent="AppTheme"/>
2.Then make a seperate styles.xml for the for version 21 (v21/styles.xml) as given below
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Settings activity theme. -->
<style name="SettingsTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">@color/sunshine_blue</item>
<item name="android:colorPrimaryDark">@color/sunshine_dark_blue</item>
</style>
</resources>
- At last in the AndroidManifest tag for your SettingsActivity you'll also want to add the settings theme:
android:theme="@style/SettingsTheme"
This would add an action bar to your Settings activity in devices having android versions grater than or equal to 21.