Settings in the Android-applications

Interested in the topic of development under Android. I am writing a small application. Faced with the fact that not fully understand how to make settings for the application. A little Googling and found an article that helped to understand. Decided to translate the article for the Russian-speaking community, including some comments to the original. 

Options are an important part of applications for Android (and not only on Android - hereinafter translator's note). It is very important - allow users to change application settings, depending on their preferences.

There are two ways to work with the settings in Android - you can create a file in a directory preferences.xml res / xml, or work with the settings from your code. In this article I'll show you how to work with the settings using preferences.xml file.
Elements of the settings have the following attributes:
  • android: key - the name of the settings on potoromu in the future can get its value
  • android: title - the title of the item set
  • android: summary - a brief description of the item set
  • android: defaultValue - default value


Now available in the following types of components settings:
  • CheckBoxPreference - a simple checkbox, which returns true or false.
  • ListPreference - radio group (radioGroup), of which can be selected only one item. Attribute android: entries points to an array with values ​​in res / values ​​/ arrays.xml, and android: entryValues ​​an array of signatures.
  • EditTextPreference - displays a dialog box with the input field. Returns a string as its value.
  • RingtonePreference - a group of radio buttons to choose a ringtone.
  • Preference - setting, working as a button.
  • PreferenceScreen - screen settings. When one PreferenceScreen nested in another, it opens a new screen with the settings.
  • PreferenceCategory - category settings.


Screen with the settings
EditTextPreference
ListPreference
RingtonePreference
PreferenceScreen


Screenshots above are generated using the following preferences.xml:

<?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="First Category"> <CheckBoxPreference android:title="Checkbox Preference" android:defaultValue="false" android:summary="This preference can be true or false" android:key="checkboxPref" /> <ListPreference android:title="List Preference" android:summary="This preference allows to select an item in a array" android:key="listPref" android:defaultValue="digiGreen" android:entries="@array/listArray" android:entryValues="@array/listValues" /> </PreferenceCategory> <PreferenceCategory android:title="Second Category"> <EditTextPreference android:name="EditText Preference" android:summary="This allows you to enter a string" android:defaultValue="Nothing" android:title="Edit This Text" android:key="editTextPref" /> <RingtonePreference android:name="Ringtone Preference" android:summary="Select a ringtone" android:title="Ringtones" android:key="ringtonePref" /> <PreferenceScreen android:key="SecondPrefScreen" android:title="Second PreferenceScreen" android:summary="This is a second PreferenceScreen"> <EditTextPreference android:name="An other EditText Preference" android:summary="This is a preference in the second PreferenceScreen" android:title="Edit text" android:key="SecondEditTextPref" /> </PreferenceScreen> <Preference android:title="Custom Preference" android:summary="This works almost like a button" android:key="customPref" /> </PreferenceCategory> </PreferenceScreen>


Attributes android: entries and android: entryValues ​​ListPreference a link to @ array / listArray and @ array / listValues ​​respectively. The values ​​are taken from the res / values ​​/ arrays.xml, which in this case is as follows:

<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="listArray"> <item>Number 1</item> <item>Number 2</item> <item>Number 3</item> <item>Number 4</item> </string-array> <string-array name="listValues"> <item>1</item> <item>2</item> <item>3</item> <item>4</item> </string-array> </resources>


In order to show the user screen with the settings to create nebhodimo aktiviti inherited from PreferenceActivity.Aktiviti example:

package org.kaloer.preferenceexample; import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.Preference.OnPreferenceClickListener; import android.widget.Toast; public class Preferences extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); // Get the custom preference Preference customPref = (Preference) findPreference("customPref"); customPref .setOnPreferenceClickListener(new OnPreferenceClickListener() { public boolean onPreferenceClick(Preference preference) { Toast.makeText(getBaseContext(), "The custom preference has been clicked", Toast.LENGTH_LONG).show(); SharedPreferences customSharedPreference = getSharedPreferences( "myCustomSharedPrefs", Activity.MODE_PRIVATE); SharedPreferences.Editor editor = customSharedPreference .edit(); editor.putString("myCustomPref", "The preference has been clicked"); editor.commit(); return true; } }); } }


A call to the settings aktiviti by clicking on the button on our main aktiviti:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button prefBtn = (Button) findViewById(R.id.prefButton); prefBtn.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent settingsActivity = new Intent(getBaseContext(), Preferences.class); startActivity(settingsActivity); } }); }


To use the settings values ​​exposed, add a method getPrefs () in the main aktiviti you want to call in the eventonStart (). You must use exactly onStart (), but not onCreate (), to be sure that the current settings are used, rather than those that were at the time the Glan aktiviti. Our method getPrefs () might look something like this:

 boolean CheckboxPreference; String ListPreference; String editTextPreference; String ringtonePreference; String secondEditTextPreference; String customPref; private void getPrefs() { // Get the xml/preferences.xml preferences SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(getBaseContext()); CheckboxPreference = prefs.getBoolean("checkboxPref", true); ListPreference = prefs.getString("listPref", "nr1"); editTextPreference = prefs.getString("editTextPref", "Nothing has been entered"); ringtonePreference = prefs.getString("ringtonePref", "DEFAULT_RINGTONE_URI"); secondEditTextPreference = prefs.getString("SecondEditTextPref", "Nothing has been entered"); // Get the custom preference SharedPreferences mySharedPreferences = getSharedPreferences( "myCustomSharedPrefs", Activity.MODE_PRIVATE); customPref = mySharedPreferences.getString("myCusomPref", ""); }


And lastly, do not forget to add aktiviti created with the settings in androidmanifest.xml and add a new row with the name «set_preferences», to refer to the title screen with the settings, for example, «Preferences».

<activity android:name=".Preferences" android:label="@string/set_preferences"> </activity>


As a result, we obtain the following:
Screen with the main aktiviti
Aktiviti with the settings