How to add ToolBar in PreferenceActivity?
Asked Answered
A

7

19

I want to add ToolBar in PreferenceActivity in my android application. I wrote the following code.

  public class SettingsActivity extends PreferenceActivity  {
 SendSMS sms;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
    LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
    android.support.v7.widget.Toolbar bar = (android.support.v7.widget.Toolbar) LayoutInflater.from(this).inflate(R.layout.action_bar_setting, root, false);
    root.addView(bar, 0);
    bar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
        }
    });

}

This worked perfectly in my android Kitkat phone API 19 but force closed in API level 10 i.e. gingerbread. Please suggest me.

Aniela answered 9/5, 2015 at 11:49 Comment(0)
H
35

You need a layout that contains a Toolbar, and a ListView with android:id="@android:id/list"

activity_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/content_frame"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" />
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {

    private AppCompatDelegate mDelegate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getDelegate().installViewFactory();
        getDelegate().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        addPreferencesFromResource(R.xml.preferences);
        ...
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        getDelegate().onPostCreate(savedInstanceState);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        getDelegate().setContentView(layoutResID);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        getDelegate().onPostResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        getDelegate().onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getDelegate().onDestroy();
    }

    private void setSupportActionBar(@Nullable Toolbar toolbar) {
        getDelegate().setSupportActionBar(toolbar);
    }

    private AppCompatDelegate getDelegate() {
        if (mDelegate == null) {
            mDelegate = AppCompatDelegate.create(this, null);
        }
        return mDelegate;
    }
    ...
}

Check out my fully working example:

Reference from the Android team: AppCompatPreferenceActivity

Helbon answered 9/5, 2015 at 12:54 Comment(7)
Hidro, why the layout needs a ListView?Seal
Work for me after add the ListView in layout, but the action bar was below the layout.Seal
PreferenceActivity requires a ListView with android:id/list in the layout if you override the layout, that's why.Helbon
Now I could make it work. Thank you, @hidro, for your explanation.Seal
doesn't work for me... It does shows the toolbar but its under the preference layout.Utimer
On Android 4.4 I'm loosing paddings from the list, so the activity doesn't look like native.Keen
there is no setSupportActionBar method on PreferenceActivityCrawl
D
9

Try:

public class SettingsActivity extends AppCompatPreferenceActivity {
.....

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupActionBar();
    /* getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new GeneralPreferenceFragment())
            .commit();
    */

    //addPreferencesFromResource(R.xml.pref_general);        
}

private void setupActionBar() {
    ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat

    if (rootView != null) {
        View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false);
        rootView.addView(view, 0);

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        // Show the Up button in the action bar.
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

app_bar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>
Dutyfree answered 14/7, 2016 at 9:53 Comment(3)
What is AppCompatPreferenceActivity ?Gig
@Eslam, it's a Class that extends Preference Activity. 'New' -> 'Settings Activity' will make it show up.Nous
What do you have in @style/AppTheme.AppBarOverlay and @style/AppTheme.PopupOverlay files?Rusk
W
4

I'm late but here's a small fix to the problem instead of writing tons of code or adding bulky libraries.

Go to AndroidManifest.xml and to your Preference Activity add the custom theme

 <activity
        android:name=".Dashboard.PreferencepActivity"
        android:label="@string/title_activity_preference"
        android:theme="@style/Pref"></activity>

Here's the custom theme added to the above @style/Pref

 <style name="Pref" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

Hope it helps!

Woke answered 13/5, 2018 at 15:17 Comment(0)
L
3

You can easily add toolbar from @android:style/Theme.Material.Light.DarkActionBar

In AndroidManifest.xml :

<activity
    android:name=".activity.SettingsActivity"
    android:theme="@style/SettingsTheme"
    android:label="Settings"/>

In v21/styles.xml

<style name="SettingsTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
<item name="android:colorPrimary">@color/colorPrimary</item>
<item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>

In v14/styles.xml for Back API support

<style name="SettingsTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/ActionBar.V14.Movie.NoTitle</item>

Lilalilac answered 16/12, 2016 at 18:33 Comment(0)
L
2

Very easy and working well in my case by inflating custom toolbar. In your java code do as below,

   public class SettingsPrefActivity extends AppCompatPreferenceActivity {
        //  private static final String TAG = SettingsPrefActivity.class.getSimpleName();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            //setting up toolbar
            getLayoutInflater().inflate(R.layout.toolbar_setting, (ViewGroup) findViewById(android.R.id.content));
            Toolbar toolbar = findViewById(R.id.toolbar);
            toolbar.setTitle("Settings");
            setSupportActionBars(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            // load settings fragment
            getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPreferenceFragment()).commit();
        }

    }

and at your xml side code,add one preference category at the top do as below,

  <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
//put below line at the top of your xml preference layout screen..
    <PreferenceCategory android:layout="@layout/toolbar_setting"></PreferenceCategory>

and in your layout resource folder toolbar_setting,

   <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="@dimen/appbar_elevation"
    android:minHeight="?attr/actionBarSize"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
Lupelupee answered 16/12, 2017 at 8:42 Comment(0)
J
1

This method will not solve adding toolbar, but you can get the default ActionBar back for your settings page. Create a theme for settings activity, by inheriting from parent theme.

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar.Bridge">
    <!-- Customize your theme here. -->
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.Settings" parent="AppTheme">
    <item name="windowNoTitle">false</item>
    <item name="windowActionBar">true</item>
</style>

In AndroidManifest.xml set the android:theme

<activity
    android:name=".Settings"
    android:theme="@style/AppTheme.Settings" />

That is all.

Jarid answered 30/8, 2018 at 12:42 Comment(0)
P
0

Instead of:

public class PreferencesActivity extends Activity

Do this:

public class PreferencesActivity extends AppCompatActivity
Pedraza answered 17/4, 2017 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.