Android nested in PreferenceScreen with ActionBar

I have a SettingsActivity app in my Android app. Originally there was no Actionbar, so I took advantage of this:

settings_toolbar.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" app:navigationContentDescription="@string/abc_action_bar_up_description" android:background="?attr/colorPrimary" app:navigationIcon="?attr/homeAsUpIndicator" app:title="@string/action_settings" /> 

SettingsActivity.java

 public class SettingsActivity extends PreferenceActivity { @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent(); Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false); root.addView(bar, 0); // insert at top bar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } } 

It works great, but only for the first PreferenceScreen. If I have a nested PreferenceScreen, then there is no ActionBar. How can I achieve this in order to have a nested PreferenceScreen ActionBar with a back button too?

It must be compatible with API15 + and AppCombat

Original post: How to add an action bar from the support library to PreferenceActivity?

+6
source share
1 answer

Instead of using a nested PreferenceScreen, we can use a simple Clickable Preference and make it work as if it were a "nested header"; this will show a regular ActionBar as it launches an instance of PreferenceActivity and therefore will also support a single panel / double panel navigation style. Here's a simplified simplified code example that includes customizing the BackBar navigation button:

main_preferences.xml

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:orderingFromXml="true"> <Preference android:key="a_preference" /> <!-- this is our "nested header", a simple Preference --> <Preference android:key="subscreen_preference" /> <Preference android:key="another_ preference" /> </PreferenceSreen> 

subscreen_preference.xml

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" android:orderingFromXml="true"> <Preference android:key="sub_preference" /> <!-- etc --> </PreferenceSreen> 

MyPreferenceActivity.class

 public class MyPreferenceActivity extends AppCompatPreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //display back button. Fragments will handle its behavior (see below) getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public void onBuildHeaders(List<Header> target) { loadHeadersFromResource(R.xml.pref_headers, target); } @Override protected boolean isValidFragment(String fragmentName) { return MainPreferenceFragment.class.getName().equals(fragmentName) || SubscreenFragment.class.getName().equals(fragmentName); } public static class MainPreferenceFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { //let the fragment intercept the ActionBar buttons: setHasOptionsMenu(true); addPreferencesFromResource(R.xml.main_preferences); findPreference("subscreen_preference").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { @Override public boolean onPreferenceClick(Preference preference) { //we create a Header manually: Header header = new Header(); //mandatory fragment name: header.fragment = "com.foo.MyPreferenceActivity$SubscreenFragment"; //subscreen title to be shown in the ActionBar header.titleRes = R.string.settings_fragment_title; //this will do the trick, no further action required: //we can ignore the second parameter ((MyPreferenceActivity)getActivity()).onHeaderClick(header, 0); return true; } }); } //this will make the ActionBar back navigation button // behave like the system back button @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { if (!super.onOptionsItemSelected(item)) { getActivity().onBackPressed(); } return true; } return super.onOptionsItemSelected(item); } } public static class SubscreenFragment extends PreferenceFragment { //usual implementation } } 

It is important . If you use Proguard, be sure to add the following rule, otherwise isInvalidFragment () will return false:

 -keepnames class com.foo.MyPreferenceActivity$SubscreenFragment 
+2
source

Source: https://habr.com/ru/post/987724/


All Articles