Here is my preference activity:
package com.example.hms.test;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class PrefsActivity extends PreferenceActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
here I want to show an actionbar with name settings and a back button to home
The answer Pooya gave won't work for a PreferenceActivity. Instead make your class extend AppCompatActivity, and use a PreferenceFragment to load up the preference. Here is my code for settings:
Put the activity in your AndroidManifest.XML:
And now you can start the settings activity using an intent in my Main Activity (or whichever parent activity you have) as normal:
You should do couple of things:
Add the following to your onCreate of PreferenceActivity:
Override onOptionsItemSelected in PreferenceActivity:
change the
<activity>
tag in manifest for your PreferenceActivity to look something like this:Finally put android:launchMode="singleTop" in your MainActivity
<activity>
tag in manifest:I needed different action bar menu items in my activities so I created my MainActivity with a singleTop launchMode. That was great for getting my child activity's action bar set up, but it left my preferences activity without an action bar.
In the end, the key was in making sure the MainActivity theme had a parent of Theme.AppCompat.Light.NoActionBar:
and the SettingsActivity theme had the parent Theme.AppCompat.Light.DarkActionBar:
In styles.xml
There's probably a better way to style it but this works.
For anyone else reading, also remember to derive your SettingsActivity from AppCompatPreferenceActivity:
SettingsActivity.java:
okay. now you still trouble then i have unique solution for you which work 100% with just minor changes.
so first of all create one style for only settings activity.
here is my toolbar styles code.
and here is my stlyes.xml looks like
yes. you noticed i have create duplicate styles(appTheme and Toolbar_settings_style both are same styles attribute) for only settingsActivity. it is very important to create duplicate styles.
Now go to your settingsActivity and paste below code inside onCreate().
After placing above code now my SettingsActivity looks like
Now let's come to real important part. go to manifest.xml file and find your settingsActivity and paste below code.
so here is my AndroidManifest.xml looks like
This is all you need to shows Toolbar on settingsActivity.