Android – Settings Activity

This post will show how to create a Settings Activity using Preference and PreferenceFragment classes.

  • Right-click the res directory and select New > Android Resource Directory. Select Resource Type the XML value and click OK
how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ
  • Right-click the newly created xml directory and then New > XML resource file. Specify the file name and click OK
how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ
  • Now add the preference views you want for your settings activity.
how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ
  • You need to specify the key and the title. The key is used to store the setting value
how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ
how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ
  • If you want to keep compatibility with the old versions you need to use PreferenceFragmentCompat and the compat views in the xml, and declare preferenceTheme. First, you need to add the dependency in the build.gradle(Module:app)
implementation 'com.android.support:preference-v7:28.0.0'

Then you have to change the views :

how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ

Open the styles.xml and add the following line as a child of AppTheme theme

how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ
  • Now, add an Empty Activity, specify the name, uncheck Generate Layout File and check Backwards Compatibility. Click Finish
how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ
  • Add a Blank Fragment for the settings, specify a name uncheck Create layout XML and all the other check boxes. Click Finish
how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ
  • Now the fragment you created, has to extend the PreferenceFragmentCompat class. Remove the onCreateView() method and the constructor. Then press Alt + Enter on the class declaration and choose Implement Methods. Select OnCreatePreferences and click OK. Now you have to associate the Fragment with the xml file you created:
setPreferencesFromResource(R.xml.preferences, s);

The code should be like this

how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ
  • Now open the SettingsActivity and load the Fragment insice onCreate() method.
getSupportFragmentManager().beginTransaction().
                replace(android.R.id.content, new SettingsFragment()).commit();

If you want to save the default values in shared preferences then use:

PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

If you want to get the preferences settings then use:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
Boolean switchPref = pref.getBoolean(SettingsActivity.KEY_SHOW_DETAILS,false);
String howManyPref = pref.getString(SettingsActivity.KEY_HOW_MANY,"");

KEY_SHOW_DETAILS and KEY_HOW_MANY are declared in the SettingsActivity and they are the keys used in the preferences.xml

how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ

If you want the preferences xml to show the values, add the two following methods in the SettingsFragment

how to implement a single settings activity, <span>Android – Settings Activity</span>, KandZ

Then add the following to show/bind the values to the xml:

bindPreferenceSummaryToValue(findPreference(SettingsActivity.KEY_HOW_MANY));

THE END!!!

97680cookie-checkAndroid – Settings ActivityAndroid – Settings Activity

Related posts

One Thought to “Android – Settings Activity

Comments are closed.