יצירת היררכיה בקוד חלק מ-Android Jetpack.

אפשר ליצור היררכיה באופן פרוגרמטי ב- onCreatePreferences() הדוגמה הבאה ממחישה גישה פרוגרמטית ליצירת במסך ההעדפות שנוצר באמצעות XML סקירה כללית. שפת תרגום ליצור את המסך באופן פרוגרמטי, ליצור כל הגדרה ולקבוע את הרלוונטיות שלה ומוסיפים אותו למסך ההעדפות:

Kotlin

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    val context = preferenceManager.context
    val screen = preferenceManager.createPreferenceScreen(context)

    val notificationPreference = SwitchPreferenceCompat(context)
    notificationPreference.key = "notifications"
    notificationPreference.title = "Enable message notifications"

    val feedbackPreference = Preference(context)
    feedbackPreference.key = "feedback"
    feedbackPreference.title = "Send feedback"
    feedbackPreference.summary = "Report technical issues or suggest new features"

    screen.addPreference(notificationPreference)
    screen.addPreference(feedbackPreference)

    preferenceScreen = screen
}

Java

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    Context context = getPreferenceManager().getContext();
    PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);

    SwitchPreferenceCompat notificationPreference = new SwitchPreferenceCompat(context);
    notificationPreference.setKey("notifications");
    notificationPreference.setTitle("Enable message notifications");

    Preference feedbackPreference = new Preference(context);
    feedbackPreference.setKey("feedback");
    feedbackPreference.setTitle("Send feedback");
    feedbackPreference.setSummary("Report technical issues or suggest new features");

    screen.addPreference(notificationPreference);
    screen.addPreference(feedbackPreference);

    setPreferenceScreen(screen);
}

הוספת PreferenceCategory הוא דומה. הדוגמה הבאה ממחישה גישה פרוגרמטית ליצירת מסך ההעדפות שמוצג לארגן את ההגדרות. הצאצאים מתווספים אל PreferenceCategory ולא לשורש PreferenceScreen.

Kotlin

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    val context = preferenceManager.context
    val screen = preferenceManager.createPreferenceScreen(context)

    val notificationPreference = SwitchPreferenceCompat(context)
    notificationPreference.key = "notifications"
    notificationPreference.title = "Enable message notifications"

    val notificationCategory = PreferenceCategory(context)
    notificationCategory.key = "notifications_category"
    notificationCategory.title = "Notifications"
    screen.addPreference(notificationCategory)
    notificationCategory.addPreference(notificationPreference)

    val feedbackPreference = Preference(context)
    feedbackPreference.key = "feedback"
    feedbackPreference.title = "Send feedback"
    feedbackPreference.summary = "Report technical issues or suggest new features"

    val helpCategory = PreferenceCategory(context)
    helpCategory.key = "help"
    helpCategory.title = "Help"
    screen.addPreference(helpCategory)
    helpCategory.addPreference(feedbackPreference)

    preferenceScreen = screen
}

Java

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    Context context = getPreferenceManager().getContext();
    PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);

    SwitchPreferenceCompat notificationPreference = new SwitchPreferenceCompat(context);
    notificationPreference.setKey("notifications");
    notificationPreference.setTitle("Enable message notifications");

    PreferenceCategory notificationCategory = new PreferenceCategory(context);
    notificationCategory.setKey("notifications_category");
    notificationCategory.setTitle("Notifications");
    screen.addPreference(notificationCategory);
    notificationCategory.addPreference(notificationPreference);

    Preference feedbackPreference = new Preference(context);
    feedbackPreference.setKey("feedback");
    feedbackPreference.setTitle("Send feedback");
    feedbackPreference.setSummary("Report technical issues or suggest new features");

    PreferenceCategory helpCategory = new PreferenceCategory(context);
    helpCategory.setKey("help");
    helpCategory.setTitle("Help");
    screen.addPreference(helpCategory);
    helpCategory.addPreference(feedbackPreference);

    setPreferenceScreen(screen);
}