আপনি onCreatePreferences() এ প্রোগ্রাম্যাটিকভাবে একটি অনুক্রম তৈরি করতে পারেন। নিম্নলিখিত উদাহরণটি ওভারভিউ পৃষ্ঠায় XML এর মাধ্যমে তৈরি একই পছন্দের স্ক্রীন তৈরি করার জন্য একটি প্রোগ্রাম্যাটিক পদ্ধতির প্রদর্শন করে। স্ক্রিনটি প্রোগ্রাম্যাটিকভাবে তৈরি করতে, প্রতিটি সেটিং তৈরি করুন এবং এর প্রাসঙ্গিক বৈশিষ্ট্যগুলি সেট করুন, তারপর এটি পছন্দের স্ক্রিনে যুক্ত করুন:
কোটলিন
overridefunonCreatePreferences(savedInstanceState:Bundle?,rootKey:String?){valcontext=preferenceManager.contextvalscreen=preferenceManager.createPreferenceScreen(context)valnotificationPreference=SwitchPreferenceCompat(context)notificationPreference.key="notifications"notificationPreference.title="Enable message notifications"valfeedbackPreference=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}
জাভা
@OverridepublicvoidonCreatePreferences(BundlesavedInstanceState,StringrootKey){Contextcontext=getPreferenceManager().getContext();PreferenceScreenscreen=getPreferenceManager().createPreferenceScreen(context);SwitchPreferenceCompatnotificationPreference=newSwitchPreferenceCompat(context);notificationPreference.setKey("notifications");notificationPreference.setTitle("Enable message notifications");PreferencefeedbackPreference=newPreference(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 নয়।
কোটলিন
overridefunonCreatePreferences(savedInstanceState:Bundle?,rootKey:String?){valcontext=preferenceManager.contextvalscreen=preferenceManager.createPreferenceScreen(context)valnotificationPreference=SwitchPreferenceCompat(context)notificationPreference.key="notifications"notificationPreference.title="Enable message notifications"valnotificationCategory=PreferenceCategory(context)notificationCategory.key="notifications_category"notificationCategory.title="Notifications"screen.addPreference(notificationCategory)notificationCategory.addPreference(notificationPreference)valfeedbackPreference=Preference(context)feedbackPreference.key="feedback"feedbackPreference.title="Send feedback"feedbackPreference.summary="Report technical issues or suggest new features"valhelpCategory=PreferenceCategory(context)helpCategory.key="help"helpCategory.title="Help"screen.addPreference(helpCategory)helpCategory.addPreference(feedbackPreference)preferenceScreen=screen}
জাভা
@OverridepublicvoidonCreatePreferences(BundlesavedInstanceState,StringrootKey){Contextcontext=getPreferenceManager().getContext();PreferenceScreenscreen=getPreferenceManager().createPreferenceScreen(context);SwitchPreferenceCompatnotificationPreference=newSwitchPreferenceCompat(context);notificationPreference.setKey("notifications");notificationPreference.setTitle("Enable message notifications");PreferenceCategorynotificationCategory=newPreferenceCategory(context);notificationCategory.setKey("notifications_category");notificationCategory.setTitle("Notifications");screen.addPreference(notificationCategory);notificationCategory.addPreference(notificationPreference);PreferencefeedbackPreference=newPreference(context);feedbackPreference.setKey("feedback");feedbackPreference.setTitle("Send feedback");feedbackPreference.setSummary("Report technical issues or suggest new features");PreferenceCategoryhelpCategory=newPreferenceCategory(context);helpCategory.setKey("help");helpCategory.setTitle("Help");screen.addPreference(helpCategory);helpCategory.addPreference(feedbackPreference);setPreferenceScreen(screen);}
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[],null,["# Create a hierarchy in code\nPart of [Android Jetpack](/jetpack).\n===============================================================\n\nYou can create a hierarchy programmatically in\n[`onCreatePreferences()`](/reference/androidx/preference/PreferenceFragmentCompat#oncreatepreferences).\nThe following example demonstrates a programmatic approach to creating the same\npreference screen created through XML on the\n[overview](/develop/ui/views/components/settings#create_a_hierarchy) page. To\ncreate the screen programmatically, create each setting and set its relevant\nproperties, then add it to the preference screen: \n\n### Kotlin\n\n```kotlin\noverride fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {\n val context = preferenceManager.context\n val screen = preferenceManager.createPreferenceScreen(context)\n\n val notificationPreference = SwitchPreferenceCompat(context)\n notificationPreference.key = \"notifications\"\n notificationPreference.title = \"Enable message notifications\"\n\n val feedbackPreference = Preference(context)\n feedbackPreference.key = \"feedback\"\n feedbackPreference.title = \"Send feedback\"\n feedbackPreference.summary = \"Report technical issues or suggest new features\"\n\n screen.addPreference(notificationPreference)\n screen.addPreference(feedbackPreference)\n\n preferenceScreen = screen\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onCreatePreferences(Bundle savedInstanceState, String rootKey) {\n Context context = getPreferenceManager().getContext();\n PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);\n\n SwitchPreferenceCompat notificationPreference = new SwitchPreferenceCompat(context);\n notificationPreference.setKey(\"notifications\");\n notificationPreference.setTitle(\"Enable message notifications\");\n\n Preference feedbackPreference = new Preference(context);\n feedbackPreference.setKey(\"feedback\");\n feedbackPreference.setTitle(\"Send feedback\");\n feedbackPreference.setSummary(\"Report technical issues or suggest new features\");\n\n screen.addPreference(notificationPreference);\n screen.addPreference(feedbackPreference);\n\n setPreferenceScreen(screen);\n}\n```\n\nAdding a\n[`PreferenceCategory`](/reference/androidx/preference/PreferenceCategory) is\nsimilar. The following example demonstrates a programmatic approach to creating\nthe preference screen seen in\n[Organize your settings](/develop/ui/views/components/settings/organize-your-settings#preference_categories).\nThe children are added to the `PreferenceCategory` and not to the root\n`PreferenceScreen`. \n\n### Kotlin\n\n```kotlin\noverride fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {\n val context = preferenceManager.context\n val screen = preferenceManager.createPreferenceScreen(context)\n\n val notificationPreference = SwitchPreferenceCompat(context)\n notificationPreference.key = \"notifications\"\n notificationPreference.title = \"Enable message notifications\"\n\n val notificationCategory = PreferenceCategory(context)\n notificationCategory.key = \"notifications_category\"\n notificationCategory.title = \"Notifications\"\n screen.addPreference(notificationCategory)\n notificationCategory.addPreference(notificationPreference)\n\n val feedbackPreference = Preference(context)\n feedbackPreference.key = \"feedback\"\n feedbackPreference.title = \"Send feedback\"\n feedbackPreference.summary = \"Report technical issues or suggest new features\"\n\n val helpCategory = PreferenceCategory(context)\n helpCategory.key = \"help\"\n helpCategory.title = \"Help\"\n screen.addPreference(helpCategory)\n helpCategory.addPreference(feedbackPreference)\n\n preferenceScreen = screen\n}\n```\n\n### Java\n\n```java\n@Override\npublic void onCreatePreferences(Bundle savedInstanceState, String rootKey) {\n Context context = getPreferenceManager().getContext();\n PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(context);\n\n SwitchPreferenceCompat notificationPreference = new SwitchPreferenceCompat(context);\n notificationPreference.setKey(\"notifications\");\n notificationPreference.setTitle(\"Enable message notifications\");\n\n PreferenceCategory notificationCategory = new PreferenceCategory(context);\n notificationCategory.setKey(\"notifications_category\");\n notificationCategory.setTitle(\"Notifications\");\n screen.addPreference(notificationCategory);\n notificationCategory.addPreference(notificationPreference);\n\n Preference feedbackPreference = new Preference(context);\n feedbackPreference.setKey(\"feedback\");\n feedbackPreference.setTitle(\"Send feedback\");\n feedbackPreference.setSummary(\"Report technical issues or suggest new features\");\n\n PreferenceCategory helpCategory = new PreferenceCategory(context);\n helpCategory.setKey(\"help\");\n helpCategory.setTitle(\"Help\");\n screen.addPreference(helpCategory);\n helpCategory.addPreference(feedbackPreference);\n\n setPreferenceScreen(screen);\n}\n```\n| **Warning:** Add the `PreferenceCategory` to the `PreferenceScreen` before adding children to it. Preferences can't be added to a `PreferenceCategory` that isn't attached to the root screen."]]