I use PreferenceFragment
in ActionBarActivity
from support-v7 library.
In the Activity I have Toolbar
. Everything goes okay, until I open a nested PreferenceScreen
.
In the opened screen the Toolbar
is hidden.
Maybe somebody know a workaround for this issue?
Preferences xml-file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Main category" >
<EditTextPreference
android:defaultValue="defaultValue"
android:key="key_global_setting"
android:title="Global title" />
</PreferenceCategory>
<PreferenceCategory android:title="Nested screens" >
<PreferenceScreen
android:persistent="false"
android:title="@string/settings_facility_title" >
<CheckBoxPreference
android:defaultValue="false"
android:key="nested_screen_1_1"
android:title="Nested screen 1.1 check box" />
<CheckBoxPreference
android:defaultValue="true"
android:key="nested_screen_1_2"
android:title="Nested screen 1.2 check box" />
</PreferenceScreen>
<PreferenceScreen
android:persistent="false"
android:title="@string/settings_menu_screen_title" >
<CheckBoxPreference
android:defaultValue="true"
android:key="nested_screen2"
android:title="Nested screen 2 check box" />
</PreferenceScreen>
</PreferenceCategory>
</PreferenceScreen>
Activity layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SettingsScreen" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
style="@style/Toolbar" />
<FrameLayout
android:id="@+id/contentSettings"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here comes my solution, which is inspired by the original answer but not that complicated. Maybe it'll help someone...
layout/settings.xml
:Classes:
I tested it on
4.3
and5.0.2
and no limitation on nesting levels appliesI found the solution on my own. I used a small work-around of all this nested
PreferenceScreen's
. I simply made a separation to differentxml-preference
files, created an additionalFragment
which extendsPreferenceFragment
and there I show an appropriate nested preference screen.Maybe somebody would found this useful.
Github sources link.
Some code examples below:
main_preferences.xml
nested_screen1_preferences.xml
nested_screen2_preferences.xml
SettingsActivity.java
MyPreferenceFragment.java
NestedPreferencesFragment.java
As the issue comes from the part that you are still in the same activity/fragment and the nested pref screen is just a dialog you can do the following:
Get the root view from the dialog:
(PreferenceScreen)preference).getDialog().getWindow() .getDecorView().getRootView());
Recursively search until find a stub view (there is one, unfortunately I do not know the
android.R.id.xxxxx
) and set what layout you need as title which will look like the toolbar(You can inflate toolbar):In the layout you can put only a toolbar. And set the back icon. Register for click on it and having reference to the fragment, on click you can dismiss the dialog. You have set title and etc.
In my solution you only need one
AppCompatActivity
and onePreferenceFragement
, but several XML files, each having only one level ofPreferenceScreens
.XML file list
This code is for one sub-level (for simplicity and to get the idea), but you can easily extend it to have arbitrary sub-levels of
PreferenceScreens
.SettingsFragment.java
SettingsActivity.java
Technically it should work for all Android versions for which the
PreferenceFragment
is available.