From 9e3ae2f0bbf2b3f2a9f130a244d12855de1bc829 Mon Sep 17 00:00:00 2001 From: Akexorcist Date: Wed, 15 Apr 2020 18:40:51 +0700 Subject: [PATCH] Add example code for preference fragment Thanks #41 for example code --- app/src/main/AndroidManifest.xml | 1 + .../localizationapp/MainActivity.java | 6 +++++ .../preferences/LanguagePreferenceFragment.kt | 26 +++++++++++++++++++ .../preferences/ListPreferencesActivity.kt | 17 ++++++++++++ .../res/layout/activity_list_preferences.xml | 13 ++++++++++ app/src/main/res/layout/activity_main.xml | 6 +++++ app/src/main/res/values/settings_strings.xml | 21 +++++++++++++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/settings.xml | 13 ++++++++++ 9 files changed, 104 insertions(+) create mode 100644 app/src/main/java/com/akexorcist/localizationapp/preferences/LanguagePreferenceFragment.kt create mode 100644 app/src/main/java/com/akexorcist/localizationapp/preferences/ListPreferencesActivity.kt create mode 100644 app/src/main/res/layout/activity_list_preferences.xml create mode 100644 app/src/main/res/values/settings_strings.xml create mode 100644 app/src/main/res/xml/settings.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ce015dd..beaf2cc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -23,6 +23,7 @@ + diff --git a/app/src/main/java/com/akexorcist/localizationapp/MainActivity.java b/app/src/main/java/com/akexorcist/localizationapp/MainActivity.java index 75e4c60..5793fb6 100644 --- a/app/src/main/java/com/akexorcist/localizationapp/MainActivity.java +++ b/app/src/main/java/com/akexorcist/localizationapp/MainActivity.java @@ -7,6 +7,7 @@ import com.akexorcist.localizationactivity.ui.LocalizationActivity; import com.akexorcist.localizationapp.customactivity.SimpleCustomActivity; import com.akexorcist.localizationapp.nestedfragment.NestedFragmentActivity; +import com.akexorcist.localizationapp.preferences.ListPreferencesActivity; import com.akexorcist.localizationapp.simpleactivity.SimpleActivity; import com.akexorcist.localizationapp.simplefragment.SimpleFragmentActivity; import com.akexorcist.localizationapp.stackedactivity.Stack1Activity; @@ -25,6 +26,7 @@ public void onCreate(Bundle savedInstanceState) { findViewById(R.id.btn_activity_fragment).setOnClickListener(onSimpleFragmentClick()); findViewById(R.id.btn_activity_nested_fragment).setOnClickListener(onNestedFragmentClick()); findViewById(R.id.btn_activity_view_pager).setOnClickListener(onViewPagerClick()); + findViewById(R.id.btn_activity_list_preferences).setOnClickListener(onPreferenceFragmentClick()); } public View.OnClickListener onSimpleActivityClick() { @@ -51,6 +53,10 @@ public View.OnClickListener onViewPagerClick() { return view -> goToActivity(ViewPagerActivity.class); } + public View.OnClickListener onPreferenceFragmentClick() { + return view -> goToActivity(ListPreferencesActivity.class); + } + public void goToActivity(Class activity) { Intent intent = new Intent(this, activity); startActivity(intent); diff --git a/app/src/main/java/com/akexorcist/localizationapp/preferences/LanguagePreferenceFragment.kt b/app/src/main/java/com/akexorcist/localizationapp/preferences/LanguagePreferenceFragment.kt new file mode 100644 index 0000000..34f81bd --- /dev/null +++ b/app/src/main/java/com/akexorcist/localizationapp/preferences/LanguagePreferenceFragment.kt @@ -0,0 +1,26 @@ +package com.akexorcist.localizationapp.preferences + +import android.os.Bundle +import androidx.preference.Preference +import androidx.preference.PreferenceFragmentCompat +import com.akexorcist.localizationapp.R + +class LanguagePreferenceFragment : PreferenceFragmentCompat() { + companion object { + private const val KEY_PREFERENCE_LANGUAGE = "language" + } + + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + setPreferencesFromResource(R.xml.settings, rootKey) + setHasOptionsMenu(false) + findPreference(KEY_PREFERENCE_LANGUAGE)?.setOnPreferenceChangeListener { _, newValue -> + val language = newValue.toString() + (activity as PreferenceChangeListener).onLanguagePreferenceChanged(language) + true + } + } + + interface PreferenceChangeListener { + fun onLanguagePreferenceChanged(langauge: String) + } +} diff --git a/app/src/main/java/com/akexorcist/localizationapp/preferences/ListPreferencesActivity.kt b/app/src/main/java/com/akexorcist/localizationapp/preferences/ListPreferencesActivity.kt new file mode 100644 index 0000000..064de2d --- /dev/null +++ b/app/src/main/java/com/akexorcist/localizationapp/preferences/ListPreferencesActivity.kt @@ -0,0 +1,17 @@ +package com.akexorcist.localizationapp.preferences + +import android.os.Bundle +import com.akexorcist.localizationactivity.ui.LocalizationActivity +import com.akexorcist.localizationapp.R + +class ListPreferencesActivity : LocalizationActivity(), LanguagePreferenceFragment.PreferenceChangeListener { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_list_preferences) + supportFragmentManager.beginTransaction().replace(R.id.layoutFragmentContainer, LanguagePreferenceFragment()).commit() + } + + override fun onLanguagePreferenceChanged(langauge: String) { + setLanguage(langauge) + } +} diff --git a/app/src/main/res/layout/activity_list_preferences.xml b/app/src/main/res/layout/activity_list_preferences.xml new file mode 100644 index 0000000..2dceda5 --- /dev/null +++ b/app/src/main/res/layout/activity_list_preferences.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3bc4636..65001b0 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -64,6 +64,12 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/viewpager_activity" /> + +