Last active
January 17, 2022 14:12
-
-
Save JoshSkrzypczak/7833f69d12b9b5f7b42cef0402845b08 to your computer and use it in GitHub Desktop.
Android Settings Screen using the v7.preference library
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
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
tools:context=".ui.settings.SettingsActivity"> | |
<FrameLayout | |
android:id="@+id/pref_container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"/> | |
</RelativeLayout> |
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
<manifest package="com.yourpackage" | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<application | |
... | |
... | |
... | |
android:theme="@style/AppTheme"> | |
... | |
... | |
<activity android:name=".ui.settings.SettingsActivity" | |
android:theme="@style/AppTheme.Settings" | |
android:label="Settings"/> | |
</application> | |
</manifest> |
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
<!--NOTE: Place this file in res/xml/--> | |
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.v7.preference.PreferenceScreen | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<!--Main bits of preferences--> | |
<android.support.v7.preference.PreferenceCategory | |
android:title="General"> | |
<android.support.v7.preference.ListPreference | |
android:defaultValue="@string/sort_by_last_name" | |
android:dialogTitle="@string/pref_sort_list_label_sortType" | |
android:entries="@array/pref_sortType_entries_lists" | |
android:entryValues="@array/pref_sortType_values_lists" | |
android:key="pref_sort_order_lists" | |
android:title="@string/pref_title_sort_lists"/> | |
<android.support.v7.preference.ListPreference | |
android:defaultValue="@string/state_indiana" | |
android:dialogTitle="@string/pref_title_home_state" | |
android:entries="@array/pref_default_state_entries_list" | |
android:entryValues="@array/pref_default_state_values_list" | |
android:key="pref_default_home_state" | |
android:summary="Preference summary here" | |
android:title="@string/pref_title_home_state"/> | |
<!--Not implemented--> | |
<android.support.v7.preference.SwitchPreferenceCompat | |
android:defaultValue="true" | |
android:key="key1" | |
android:summary="Switch Summary" | |
android:title="Switch Preference"/> | |
<!--Not implemented--> | |
<android.support.v7.preference.EditTextPreference | |
android:defaultValue="Default value" | |
android:dialogMessage="Dialog Message" | |
android:key="key2" | |
android:summary="EditText Summary" | |
android:title="EditText Preference"/> | |
<!--Not implemented--> | |
<android.support.v7.preference.CheckBoxPreference | |
android:defaultValue="true" | |
android:key="key3" | |
android:summary="CheckBox Summary" | |
android:title="CheckBox Preference"/> | |
</android.support.v7.preference.PreferenceCategory> | |
<!--Other Setting Categories--> | |
<android.support.v7.preference.PreferenceCategory | |
android:title="Other"> | |
<android.support.v7.preference.Preference | |
android:key="pref_key_licenses" | |
android:title="Licenses"/> | |
<android.support.v7.preference.Preference | |
android:key="pref_key_privacy_policy" | |
android:title="Privacy Policy"/> | |
<android.support.v7.preference.Preference | |
android:key="pref_key_terms_of_service" | |
android:title="Terms of Service"/> | |
</android.support.v7.preference.PreferenceCategory> | |
<!--Account Settings--> | |
<android.support.v7.preference.PreferenceCategory | |
android:title="Account"> | |
<android.support.v7.preference.Preference | |
android:key="pref_key_logout" | |
android:title="Logout"/> | |
<android.support.v7.preference.Preference | |
android:key="pref_key_delete_account" | |
android:title="Delete Account"/> | |
</android.support.v7.preference.PreferenceCategory> | |
</android.support.v7.preference.PreferenceScreen> |
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
public class SettingsActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_settings); | |
if (savedInstanceState == null) { | |
Fragment preferenceFragment = new SettingsFragment(); | |
FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); | |
ft.add(R.id.pref_container, preferenceFragment); | |
ft.commit(); | |
} | |
} | |
} |
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
/** | |
* The Preference Fragment which shows the Preferences as a List and handles the Dialogs for the | |
* Preferences. | |
*/ | |
public class SettingsFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceChangeListener { | |
@Override | |
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { | |
addPreferencesFromResource(R.xml.app_preferences); | |
Preference licensePref = (Preference)findPreference("pref_key_licenses"); | |
licensePref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { | |
@Override | |
public boolean onPreferenceClick(Preference preference) { | |
startActivity(new Intent(getActivity(), OpenSourceLibsActivity.class)); | |
return true; | |
} | |
}); | |
Preference logoutPref = (Preference)findPreference("pref_key_logout"); | |
logoutPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { | |
@Override | |
public boolean onPreferenceClick(Preference preference) { | |
FirebaseAuth.getInstance().signOut(); | |
startActivity(new Intent(getActivity(), LoginActivity.class)); | |
return true; | |
} | |
}); | |
/** | |
* Bind preference summary to value for lists and sorting list preferences | |
*/ | |
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_name_sort_order_lists))); | |
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_name_default_home_state))); | |
} | |
@Override | |
public boolean onPreferenceChange(Preference preference, Object newValue) { | |
setPreferenceSummary(preference, newValue); | |
if (preference.getKey().equals("pref_sort_order_lists")){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity()); | |
SharedPreferences.Editor spe = sharedPref.edit(); | |
spe.putString(Constants.KEY_PREF_SORT_ORDER_LISTS, newValue.toString()).apply(); | |
} | |
if (preference.getKey().equals("pref_default_home_state")){ | |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity()); | |
SharedPreferences.Editor spe = sharedPref.edit(); | |
spe.putString(Constants.KEY_PREF_HOME_STATE, newValue.toString()).apply(); | |
} | |
return true; | |
} | |
private void bindPreferenceSummaryToValue(Preference preference) { | |
/* Set the listener to watch for value changes. */ | |
preference.setOnPreferenceChangeListener(this); | |
/* Trigger the listener immediately with the preference's current value. */ | |
setPreferenceSummary(preference, | |
PreferenceManager | |
.getDefaultSharedPreferences(preference.getContext()) | |
.getString(preference.getKey(), "")); | |
} | |
private void setPreferenceSummary(Preference preference, Object value){ | |
String stringValue = value.toString(); | |
if (preference instanceof ListPreference){ | |
ListPreference listPreference = (ListPreference) preference; | |
int prefIndex = listPreference.findIndexOfValue(stringValue); | |
if (prefIndex >=0){ | |
preference.setSummary(listPreference.getEntries()[prefIndex]); | |
} | |
} | |
} | |
} |
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
<resources> | |
<!-- Base application theme. --> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |
<!-- Customize your theme here. --> | |
<item name="colorPrimary">@color/colorPrimary</item> | |
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> | |
<item name="colorAccent">@color/colorAccent</item> | |
<item name="android:textColorHint">@color/text_color_tertiary_dark</item> | |
</style> | |
<!-- SettingsActivity Theme --> | |
<style name="AppTheme.Settings" parent="Theme.AppCompat.Light.DarkActionBar"> | |
<item name="preferenceTheme">@style/PreferenceThemeOverlay.v14.Material</item> | |
</style> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment