This repository has been archived by the owner on Jun 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example code for preference fragment
Thanks #41 for example code
- Loading branch information
1 parent
1fd0122
commit 9e3ae2f
Showing
9 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/src/main/java/com/akexorcist/localizationapp/preferences/LanguagePreferenceFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Preference>(KEY_PREFERENCE_LANGUAGE)?.setOnPreferenceChangeListener { _, newValue -> | ||
val language = newValue.toString() | ||
(activity as PreferenceChangeListener).onLanguagePreferenceChanged(language) | ||
true | ||
} | ||
} | ||
|
||
interface PreferenceChangeListener { | ||
fun onLanguagePreferenceChanged(langauge: String) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
app/src/main/java/com/akexorcist/localizationapp/preferences/ListPreferencesActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".preferences.ListPreferencesActivity"> | ||
<FrameLayout | ||
android:id="@+id/layoutFragmentContainer" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingBottom="?attr/actionBarSize" /> | ||
</FrameLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<resources> | ||
<string-array name="pref_language_list_titles" translatable="false"> | ||
<item>America</item> | ||
<item>China</item> | ||
<item>Italy</item> | ||
<item>Japan</item> | ||
<item>Korea</item> | ||
<item>Portugal</item> | ||
<item>Thai</item> | ||
</string-array> | ||
<string-array name="pref_language_list_values" translatable="false"> | ||
<item>en</item> | ||
<item>zh</item> | ||
<item>it</item> | ||
<item>ja</item> | ||
<item>ko</item> | ||
<item>pt</item> | ||
<item>th</item> | ||
</string-array> | ||
</resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<PreferenceCategory android:title="@string/hello_world"> | ||
<ListPreference | ||
android:defaultValue="en" | ||
android:entries="@array/pref_language_list_titles" | ||
android:entryValues="@array/pref_language_list_values" | ||
android:key="language" | ||
android:negativeButtonText="@null" | ||
android:positiveButtonText="@null" | ||
android:title="@string/change_language" /> | ||
</PreferenceCategory> | ||
</PreferenceScreen> |