Skip to content

Instantly share code, notes, and snippets.

@getsadzeg
Last active April 17, 2019 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getsadzeg/7d8d03bf4adf634335acfb0d06b355a3 to your computer and use it in GitHub Desktop.
Save getsadzeg/7d8d03bf4adf634335acfb0d06b355a3 to your computer and use it in GitHub Desktop.
Preferences

So, let's imagine that we're making Preferences as Settings.

First, we make SettingsActivity, of course, which is the plain activity we all know of. Its XML must actually be a fragment, referring to SettingsFragment(which we'll make; extends PreferenceFragmentCompat) with android:name.

Second, we make SettingsFragment with its XML preference xml(<PreferenceScreen and nested preferences in it) in res/xml directory. And we bond the resource to a Fragment with addPreferencesFromResource method.

Then, we must not forget to add preference theme(@style/PreferenceThemeOverlay) to styles.xml, otherwise our app will crash.

Dope fact: any change to the UI in the activity of Settings(of preferences) is automatically saved into SharedPreferences.

Validating EditTextPreference inputs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment