Search code examples
javaandroidandroid-activityandroid-actionbarandroid-preferences

Add ActionBar to a PreferenceActivity


I am trying to make a Settings Activity using a PreferenceActivity. The problem is that the ActionBar won't show up no matter what I do.

I've tried getSupportActionBar, getActionBar, setActionBar. Nothing works. I see other apps settings with ActionBars. Do they not use PreferenceActivity?


Solution

  • 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.