Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android nested PreferenceScreen with ActionBar

I've got in my Android App a SettingsActivity. Originally there was no Actionbar, so I implemted 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've got a nested PreferenceScreen, then there is no ActionBar. How can I achieve this, to have on the nested PreferenceScreen an ActionBar with back button too?

It should be compatible with API15+ and AppCombat

Original post: How to add Action Bar from support library into PreferenceActivity?

like image 698
Tobi Avatar asked May 22 '15 09:05

Tobi


1 Answers

Instead of using the nested PreferenceScreen, we can use a simple clickable Preference and make it work as if it was a "nested Header"; this will show the usual ActionBar since it launches a PreferenceActivity instance and therefore will also maintain the single pane/dual pane navigation style. Here's some simplified example code, which includes ActionBar's back navigation button setup:

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
    }
}

Important: if you use Proguard, remember to add the following rule, otherwise isInvalidFragment() will return false:

-keepnames class com.foo.MyPreferenceActivity$SubscreenFragment
like image 170
devrocca Avatar answered Oct 21 '22 06:10

devrocca