{"id":27966,"date":"2024-03-16T09:31:41","date_gmt":"2024-03-16T09:31:41","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/"},"modified":"2024-03-22T12:18:43","modified_gmt":"2024-03-22T12:18:43","slug":"an-example-analysis-of-using-listpreference-in-android-programming","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/","title":{"rendered":"An example analysis of using ListPreference in Android programming."},"content":{"rendered":"<p>ListPreference is a common Preference component in Android, used to display a list of options for users to choose from. It is often used in Android programming to allow users to select different configuration settings.<\/p>\n<p>First, create a preference.xml file in the res\/xml directory to define the configuration options for ListPreference. In this file, you can define properties such as the title, key, and list of options for ListPreference.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">PreferenceScreen<\/span> <span class=\"hljs-attr\">xmlns:android<\/span>=<span class=\"hljs-string\">\"http:\/\/schemas.android.com\/apk\/res\/android\"<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">ListPreference<\/span>\r\n        <span class=\"hljs-attr\">android:key<\/span>=<span class=\"hljs-string\">\"list_preference\"<\/span>\r\n        <span class=\"hljs-attr\">android:title<\/span>=<span class=\"hljs-string\">\"List Preference\"<\/span>\r\n        <span class=\"hljs-attr\">android:summary<\/span>=<span class=\"hljs-string\">\"Select an option\"<\/span>\r\n        <span class=\"hljs-attr\">android:entries<\/span>=<span class=\"hljs-string\">\"@array\/options\"<\/span>\r\n        <span class=\"hljs-attr\">android:entryValues<\/span>=<span class=\"hljs-string\">\"@array\/option_values\"<\/span>\r\n        <span class=\"hljs-attr\">android:defaultValue<\/span>=<span class=\"hljs-string\">\"1\"<\/span> \/&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">PreferenceScreen<\/span>&gt;<\/span>\r\n<\/code><\/pre>\n<p>In the XML file above, the key attribute of ListPreference is used to identify the unique identifier of the Preference, making it easier to manipulate in the code. The title attribute is used to set the title of the Preference, the summary attribute is used to set the summary of the Preference. The entries attribute is used to set the display text of the options, while the entryValues attribute is used to set the corresponding values of the options. The defaultValue attribute is used to set the default value of the Preference.<\/p>\n<p>Then, create a file called arrays.xml in the res\/values directory to define the text and corresponding values for the list of options.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">resources<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">string-array<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"options\"<\/span>&gt;<\/span>\r\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">item<\/span>&gt;<\/span>Option 1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">item<\/span>&gt;<\/span>\r\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">item<\/span>&gt;<\/span>Option 2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">item<\/span>&gt;<\/span>\r\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">item<\/span>&gt;<\/span>Option 3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">item<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">string-array<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">string-array<\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"option_values\"<\/span>&gt;<\/span>\r\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">item<\/span>&gt;<\/span>1<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">item<\/span>&gt;<\/span>\r\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">item<\/span>&gt;<\/span>2<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">item<\/span>&gt;<\/span>\r\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">item<\/span>&gt;<\/span>3<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">item<\/span>&gt;<\/span>\r\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">string-array<\/span>&gt;<\/span>\r\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">resources<\/span>&gt;<\/span>\r\n<\/code><\/pre>\n<p>In MainActivity, you can use PreferenceManager to retrieve and manipulate ListPreference.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">MainActivity<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title class_\">AppCompatActivity<\/span> {\r\n\r\n    <span class=\"hljs-keyword\">private<\/span> ListPreference mListPreference;\r\n\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-keyword\">protected<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">onCreate<\/span><span class=\"hljs-params\">(Bundle savedInstanceState)<\/span> {\r\n        <span class=\"hljs-built_in\">super<\/span>.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        \r\n        mListPreference = (ListPreference) findPreference(<span class=\"hljs-string\">\"list_preference\"<\/span>);\r\n        mListPreference.setOnPreferenceChangeListener(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">Preference<\/span>.OnPreferenceChangeListener() {\r\n            <span class=\"hljs-meta\">@Override<\/span>\r\n            <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-type\">boolean<\/span> <span class=\"hljs-title function_\">onPreferenceChange<\/span><span class=\"hljs-params\">(Preference preference, Object newValue)<\/span> {\r\n                <span class=\"hljs-comment\">\/\/ \u5904\u7406\u9009\u9879\u503c\u7684\u66f4\u6539\u4e8b\u4ef6<\/span>\r\n                <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-literal\">true<\/span>;\r\n            }\r\n        });\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>In the above code, the ListPreference instance is obtained through the findPreference method, and an OnPreferenceChangeListener is set to listen for changes in the selected option.<\/p>\n<p>The above is an example analysis of how to use ListPreference. With ListPreference, it is easy to implement the functionality of allowing users to select configuration options, and to listen for changes in option values in the code, and handle them accordingly.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ListPreference is a common Preference component in Android, used to display a list of options for users to choose from. It is often used in Android programming to allow users to select different configuration settings. First, create a preference.xml file in the res\/xml directory to define the configuration options for ListPreference. In this file, you [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-27966","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>An example analysis of using ListPreference in Android programming. - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An example analysis of using ListPreference in Android programming.\" \/>\n<meta property=\"og:description\" content=\"ListPreference is a common Preference component in Android, used to display a list of options for users to choose from. It is often used in Android programming to allow users to select different configuration settings. First, create a preference.xml file in the res\/xml directory to define the configuration options for ListPreference. In this file, you [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T09:31:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T12:18:43+00:00\" \/>\n<meta name=\"author\" content=\"Liam\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Liam\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/\"},\"author\":{\"name\":\"Liam\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\"},\"headline\":\"An example analysis of using ListPreference in Android programming.\",\"datePublished\":\"2024-03-16T09:31:41+00:00\",\"dateModified\":\"2024-03-22T12:18:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/\"},\"wordCount\":275,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/\",\"name\":\"An example analysis of using ListPreference in Android programming. - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T09:31:41+00:00\",\"dateModified\":\"2024-03-22T12:18:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"An example analysis of using ListPreference in Android programming.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671\",\"name\":\"Liam\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g\",\"caption\":\"Liam\"},\"sameAs\":[\"http:\/\/Wilson\"],\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"An example analysis of using ListPreference in Android programming. - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/","og_locale":"en_US","og_type":"article","og_title":"An example analysis of using ListPreference in Android programming.","og_description":"ListPreference is a common Preference component in Android, used to display a list of options for users to choose from. It is often used in Android programming to allow users to select different configuration settings. First, create a preference.xml file in the res\/xml directory to define the configuration options for ListPreference. In this file, you [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T09:31:41+00:00","article_modified_time":"2024-03-22T12:18:43+00:00","author":"Liam","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Liam","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/"},"author":{"name":"Liam","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671"},"headline":"An example analysis of using ListPreference in Android programming.","datePublished":"2024-03-16T09:31:41+00:00","dateModified":"2024-03-22T12:18:43+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/"},"wordCount":275,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/","url":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/","name":"An example analysis of using ListPreference in Android programming. - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T09:31:41+00:00","dateModified":"2024-03-22T12:18:43+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/an-example-analysis-of-using-listpreference-in-android-programming\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"An example analysis of using ListPreference in Android programming."}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/23786905eb7b377f45ddb01c17da7671","name":"Liam","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/8d37ed3e7f770dde8bf069ba0b4298688028c3abaacf1131742fc1352d174ebd?s=96&d=mm&r=g","caption":"Liam"},"sameAs":["http:\/\/Wilson"],"url":"https:\/\/www.silicloud.com\/blog\/author\/liamwilson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/27966","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=27966"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/27966\/revisions"}],"predecessor-version":[{"id":62233,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/27966\/revisions\/62233"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=27966"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=27966"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=27966"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}