{"id":163,"date":"2016-06-22T07:51:15","date_gmt":"2016-06-22T07:51:15","guid":{"rendered":"https:\/\/wp.local\/?p=163"},"modified":"2016-06-23T05:56:16","modified_gmt":"2016-06-23T05:56:16","slug":"multi-language-support-and-spinnerpreference-part-ii","status":"publish","type":"post","link":"https:\/\/blog.c-devtech.net\/?p=163","title":{"rendered":"Multi-Language support and SpinnerPreference &#8211; Part II"},"content":{"rendered":"<p>As mentioned in <a href=\"https:\/\/wp.local\/?p=153\">Part I<\/a>, calling <em>updateConfiguration<\/em> with the correct parameters for the locale provides a simple way to switch the language used in an app. Of course, the app should always start with the language last selected by the user and should provide an easy method with which to change the displayed language.<\/p>\n<p>Saving state in an Android app can take many forms. For short term, such as handling orientation changes, using<a href=\"https:\/\/developer.android.com\/training\/basics\/activity-lifecycle\/recreating.html\" target=\"_blank\"> savedInstanceState<\/a> in <em>onCreate <\/em>or a <a href=\"http:\/\/luboganev.github.io\/blog\/headless-fragments\/\" target=\"_blank\">Headless Retained Fragment <\/a> works very well. However these methods are not appropriate for user settings which require a persistent storage that spans the app restarting not just the activity. For this type of storage in Android there is a built-in database, <a href=\"http:\/\/www.tutorialspoint.com\/android\/android_sqlite_database.htm\" target=\"_blank\">SQLite<\/a>, the <a href=\"https:\/\/developer.android.com\/training\/basics\/data-storage\/files.html\" target=\"_blank\">file system<\/a>, or <a href=\"http:\/\/androidopentutorials.com\/android-sharedpreferences-tutorial-and-example\/\" target=\"_blank\">SharedPreferences<\/a>, which I ended up selecting since I was going to use a <a href=\"https:\/\/developer.android.com\/reference\/android\/preference\/PreferenceFragment.html\" target=\"_blank\">PreferenceFragment<\/a> for my user settings interface.<\/p>\n<p><strong>To implement a PreferenceFragment<\/strong> create a <em>SettingsActivity <\/em>which extends <em>AppCompatActivity <\/em>and include the innerClass &#8211; <em>MyPreferenceFragmen<\/em>t which extends the <em>PreferenceFragment <\/em>and loads the xml file that defines the preferences.<\/p>\n<pre>public class SettingsActivity extends AppCompatActivity {\r\n\r\n    @Override\r\n    public void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n\r\n        setContentView(R.layout.activity_settings);\r\n\r\n        getFragmentManager().beginTransaction().replace(\r\n                R.id.content_frame, \r\n                new MyPreferenceFragment()).commit();\r\n    }<\/pre>\n<pre>public static class MyPreferenceFragment extends PreferenceFragment{\r\n    @Override\r\n    public void onCreate(final Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n\r\n        \/\/ Define the xml file used for preferences\r\n        addPreferencesFromResource(R.xml.preferences);\r\n    }\r\n}<\/pre>\n<p>The <em>activity_settings.xml<\/em> file is very simple and is used as the fragment container:<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;FrameLayout xmlns:android=\r\n        \"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    android:id=\"@+id\/content_frame\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"&gt;\r\n&lt;\/FrameLayout&gt;<\/pre>\n<p>The <em>preferences.xml<\/em> file provides the layout for the <a href=\"https:\/\/developer.android.com\/reference\/android\/preference\/PreferenceScreen.html\" target=\"_blank\">PreferenceScreen<\/a>. The built-in widgets available for the preference screen are: <a href=\"https:\/\/developer.android.com\/reference\/android\/preference\/CheckBoxPreference.html\" target=\"_blank\">CheckBoxPreference<\/a>, <a href=\"https:\/\/developer.android.com\/reference\/android\/preference\/ListPreference.html\" target=\"_blank\">ListPreference <\/a>and <a href=\"https:\/\/developer.android.com\/reference\/android\/preference\/EditTextPreference.html\" target=\"_blank\">EditTextPreference<\/a>. In part III, we&#8217;ll add a widget that&#8217;s very useful for selecting an item from a list, the <strong>SpinnerPreference<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As mentioned in Part I, calling updateConfiguration with the correct parameters for the locale provides a simple way to switch the language used in an app. Of course, the app should always start with the language last selected by the &hellip; <a href=\"https:\/\/blog.c-devtech.net\/?p=163\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[8],"tags":[],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-android-app-develoment"],"jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p1IEK3-2D","jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163"}],"version-history":[{"count":7,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":178,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions\/178"}],"wp:attachment":[{"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.c-devtech.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}