belongs to Maven artifact androidx.leanback:leanback-preference:1.0.0-alpha1

LeanbackPreferenceFragment

public abstract class LeanbackPreferenceFragment
extends BaseLeanbackPreferenceFragment

java.lang.Object
   ↳ android.app.Fragment
     ↳ androidx.preference.PreferenceFragment
       ↳ androidx.leanback.preference.BaseLeanbackPreferenceFragment
         ↳ androidx.leanback.preference.LeanbackPreferenceFragment


This fragment provides a fully decorated leanback-style preference fragment, including a list background and header.

The following sample code shows a simple leanback preference fragment that is populated from a resource. The resource it loads is:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/root_title">

    <Preference
        android:key="basic_preference"
        android:title="@string/title_basic_preference"
        android:summary="@string/summary_basic_preference" />

    <Preference
        android:key="stylish_preference"
        android:title="@string/title_stylish_preference"
        android:summary="@string/summary_stylish_preference" />

    <Preference
        android:key="preference_with_icon"
        android:title="Preference with icon"
        android:summary="This preference has an icon"
        android:icon="@android:drawable/ic_menu_camera" />

    <PreferenceCategory
        android:title="@string/inline_preferences">

        <CheckBoxPreference
            android:key="checkbox_preference"
            android:title="@string/title_checkbox_preference"
            android:summary="@string/summary_checkbox_preference" />

        <SwitchPreference
            android:key="switch_preference"
            android:title="Switch preference"
            android:summary="This is a switch" />

        <DropDownPreference
            android:key="dropdown_preference"
            android:title="@string/title_dropdown_preference"
            android:summary="@string/summary_dropdown_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference" />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/dialog_based_preferences">

        <EditTextPreference
            android:key="edittext_preference"
            android:title="@string/title_edittext_preference"
            android:summary="@string/summary_edittext_preference"
            android:dialogTitle="@string/dialog_title_edittext_preference" />

        <ListPreference
            android:key="list_preference"
            android:title="@string/title_list_preference"
            android:summary="@string/summary_list_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="@string/dialog_title_list_preference" />

        <MultiSelectListPreference
            android:key="multi_select_list_preference"
            android:title="@string/title_multi_list_preference"
            android:summary="@string/summary_multi_list_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="@string/dialog_title_multi_list_preference" />

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/launch_preferences">

        <!-- This PreferenceScreen tag serves as a screen break (similar to page break
             in word processing). Like for other preference types, we assign a key
             here so it is able to save and restore its instance state. -->
        <PreferenceScreen
            android:key="screen_preference"
            android:title="@string/title_screen_preference"
            android:summary="@string/summary_screen_preference">

            <!-- You can place more preferences here that will be shown on the next screen. -->

            <CheckBoxPreference
                android:key="next_screen_checkbox_preference"
                android:title="@string/title_next_screen_toggle_preference"
                android:summary="@string/summary_next_screen_toggle_preference" />

        </PreferenceScreen>

        <PreferenceScreen
            android:title="@string/title_intent_preference"
            android:summary="@string/summary_intent_preference">

            <intent android:action="android.intent.action.VIEW"
                android:data="http://www.android.com" />

        </PreferenceScreen>

    </PreferenceCategory>

    <PreferenceCategory
        android:title="@string/preference_attributes">

        <CheckBoxPreference
            android:key="parent_checkbox_preference"
            android:title="@string/title_parent_preference"
            android:summary="@string/summary_parent_preference" />

        <!-- The visual style of a child is defined by this styled theme attribute. -->
        <CheckBoxPreference
            android:key="child_checkbox_preference"
            android:dependency="parent_checkbox_preference"
            android:layout="?android:attr/preferenceLayoutChild"
            android:title="@string/title_child_preference"
            android:summary="@string/summary_child_preference" />

    </PreferenceCategory>

</PreferenceScreen>

The fragment needs only to implement PreferenceFragment.onCreatePreferences(Bundle, String) to populate the list of preference objects:

public static class SettingsFragment extends LeanbackSettingsFragment {
    @Override
    public void onPreferenceStartInitialScreen() {
        startPreferenceFragment(new PrefsFragment());
    }

    @Override
    public boolean onPreferenceStartFragment(PreferenceFragment caller, Preference pref) {
        final Fragment f =
                Fragment.instantiate(getActivity(), pref.getFragment(), pref.getExtras());
        f.setTargetFragment(caller, 0);
        if (f instanceof PreferenceFragment || f instanceof PreferenceDialogFragment) {
            startPreferenceFragment(f);
        } else {
            startImmersiveFragment(f);
        }
        return true;
    }

    @Override
    public boolean onPreferenceStartScreen(PreferenceFragment caller, PreferenceScreen pref) {
        final Fragment f = new PrefsFragment();
        final Bundle args = new Bundle(1);
        args.putString(PreferenceFragment.ARG_PREFERENCE_ROOT, pref.getKey());
        f.setArguments(args);
        startPreferenceFragment(f);
        return true;
    }
}

public static class PrefsFragment extends LeanbackPreferenceFragment {

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        // Load the preferences from an XML resource
        setPreferencesFromResource(R.xml.preferences, rootKey);
    }
}

Summary

Inherited XML attributes

Inherited constants

Public constructors

LeanbackPreferenceFragment()

Public methods

View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
void onViewCreated(View view, Bundle savedInstanceState)
void setTitle(CharSequence title)

Set the title to be shown above the preference list

Inherited methods

Public constructors

LeanbackPreferenceFragment

LeanbackPreferenceFragment ()

Public methods

onCreateView

View onCreateView (LayoutInflater inflater, 
                ViewGroup container, 
                Bundle savedInstanceState)

Parameters
inflater LayoutInflater

container ViewGroup

savedInstanceState Bundle

Returns
View

onViewCreated

void onViewCreated (View view, 
                Bundle savedInstanceState)

Parameters
view View

savedInstanceState Bundle

setTitle

void setTitle (CharSequence title)

Set the title to be shown above the preference list

Parameters
title CharSequence: Title text to be shown