I am trying to do something like the settings preferences in android for tablet.
When I click the "More" the the "Wireless & Networks" preferences screen is displayed on the right fragment, than if I touch the "VPN" , in the same fragment the "VPN" preferences screen is opened.
How can I do that ?
this is my preference xml
<PreferenceScreen android:title="Title A">
<PreferenceScreen android:title="TITLE B">
<PreferenceCategory
android:title="category">
<ListPreference android:key="list"
android:title="list" android:entries="@array/list_vals"
android:entryValues="@array/list_vals1"
android:defaultValue="1" android:dialogTitle="list title"
android:negativeButtonText="cancel" />
</PreferenceCategory>
</PreferenceScreen>
</PreferenceScreen>
The problem is I do not want the nested preference screen with title B to be loaded on the whole screen I just want to be loaded in the place of the right fragment...
NOTE: that I used this documentation to create my example so far http://developer.android.com/guide/topics/ui/settings.html#PreferenceHeaders
EDIT*
this is my activity
public class SettingsActivity extends PreferenceActivity {
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}
}
I do not have an layout, so how can I know what is my master fragment id and what is my detail fragment id ?
In Android apps, there are often settings pages that contain different options the user can tweak. The PreferenceFragment and PreferenceFragmentCompat contains a hierarchy of preference objects displayed on screen in a list. These preferences will automatically save to SharedPreferences as the user interacts with them.
It's still possible to customise the appearance of a Preference item though. In your XML you have to declare the root element as android:id="@android:id/widget_frame , and then declare TextView as android:title and android:summary . You can then declare other elements you want to appear in the layout.
Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. Represents a top-level Preference that is the root of a Preference hierarchy. A PreferenceActivity points to an instance of this class to show the preferences.
Short example, define a PreferenceActivity
, and provide a headers file for main categories:
Class SettingsActivity
:
public class SettingsActivity extends PreferenceActivity {
@Override
public void onBuildHeaders(List<Header> target) {
super.onBuildHeaders(target);
loadHeadersFromResource(R.xml.headers,target);
}
}
File /res/xml/headers.xml
:
<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header android:title="Preference Header"
android:fragment="com.example.SettingsDemo.PrefFragment"/>
</preference-headers>
Create a PreferenceFragment
as referenced in android:fragment
above, provide its own preferences file:
class PrefFragment
:
public class PrefFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.prefs);
}
}
File /res/xml/prefs.xml
:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Preferences">
<SwitchPreference android:title="Preference" />
<PreferenceScreen android:title="Sub Preferences"
android:fragment="com.example.SettingsDemo.SubPrefFragment"/>
</PreferenceCategory>
</PreferenceScreen>
Create a child PreferenceFragment
as referenced in android:fragment
above, provide its own preferences file too:
Class SubPrefFragment
:
public class SubPrefFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.sub_prefs);
}
}
File /res/xml/sub_prefs.xml
:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Sub Preferences">
<CheckBoxPreference android:title="Sub Preference"/>
</PreferenceCategory>
</PreferenceScreen>
That's it. The PreferenceActivity
will automatically show all the child fragments.
Results:
Main PreferenceFragment
:
Replaced by sub PreferenceFragment
, under same header:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With