Add ActionBar to a PreferenceActivity
Asked Answered
C

3

8

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?

Crowder answered 12/6, 2015 at 0:34 Comment(2)
Yes, mostly people don't use PreferenceActivity for Activity that contains settings. I use normal Activity and handles everything on my own.Ineffable
Check this one #17849693Unipolar
T
20

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.

Tumpline answered 12/6, 2015 at 6:58 Comment(0)
P
6

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.

Porous answered 16/10, 2016 at 11:8 Comment(0)
A
1

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);

        }
    }
Angel answered 21/2, 2019 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.