Home » Android » java – Add ActionBar to a PreferenceActivity

java – Add ActionBar to a PreferenceActivity

Posted by: admin June 15, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.

Answer:

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

Answer:

This is what worked for me,

simply change PreferenceActivity to AppCompatActivity and in your manifest.xml file add parentActivityName for that particular activity.

AndroidManifest.xml

 <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".HomeActivity"></activity>

SettingsActivty.java

public class SettingsActivity extends AppCompatActivity implements SharedPreferences.OnSharedPreferenceChangeListener {


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();


    }


    public static class MyPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(final Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);

        }
    }