Skip to content
This repository has been archived by the owner on Jun 22, 2022. It is now read-only.

Commit

Permalink
Add example code for preference fragment
Browse files Browse the repository at this point in the history
Thanks #41 for example code
  • Loading branch information
akexorcist committed Apr 15, 2020
1 parent 1fd0122 commit 9e3ae2f
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<activity android:name=".customactivity.SimpleCustomActivity" />
<activity android:name=".stackedactivity.Stack1Activity" />
<activity android:name=".stackedactivity.Stack2Activity" />
<activity android:name=".preferences.ListPreferencesActivity" />
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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);
Expand Down
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)
}
}
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)
}
}
13 changes: 13 additions & 0 deletions app/src/main/res/layout/activity_list_preferences.xml
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>
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/viewpager_activity" />

<Button
android:id="@+id/btn_activity_list_preferences"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/list_preferences_activity" />
</LinearLayout>
</LinearLayout>
</ScrollView>
21 changes: 21 additions & 0 deletions app/src/main/res/values/settings_strings.xml
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>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<string name="nested_fragment_activity" translatable="false">Activity + Nested Fragment</string>
<string name="viewpager_activity" translatable="false">Activity + View Pager</string>
<string name="viewpager_description" translatable="false">Swipe or click to change page</string>
<string name="list_preferences_activity" translatable="false">Activity + Preference Fragment</string>

<string name="america" translatable="false">America</string>
<string name="china" translatable="false">China</string>
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/res/xml/settings.xml
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>

0 comments on commit 9e3ae2f

Please sign in to comment.