Preferences are an important part of an Android application. It is important to let the users have the choice to modify and personalize their application depending on their needs.
In the screenshots "custom" is spelled "costum". The typo is fixed in the code examples.
Android preferences can be set in two ways. You can create a preferences.xml file in the res/xml directory, or you can set the preferences from code.
The first example shows a preferences.xml file. Every preference needs to have a android:key value, that we call to get the preference's value. The android:title is the preference's title, and the android:summary is a summary about the preference. The android:defaultValue is the default value of the preference - fx. true or false.
Currently there are 5 different preference views:
- The CheckBoxPreference is a simple checkbox, that can return true or false.
- The ListPreference, which shows a radioGroup where only 1 item can be selected a time. The android:entries links to an array in the res/values/arrays, and the android:entryValues is an other array with the items to be returned.
- The EditTextPreference shows a dialog with an editText view. This returns a String.
- The RingtonePreference shows a radioGroup that shows the ringtones.
- The Preference is a custom preference. This works like a Button.
- The PreferenceScreen is a screen with preferences. When you have a PreferenceScreen inside an other PreferenceScreen, it simply opens a new screen with other preferences.
- The PreferenceCategory is a category with preferences.
- Custom preference
- Edit Text Preference
- List Preference
- Ringtone Preference
- Preference Screen
This is an example on the preferences.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| <?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> |
To show the preference screen, we create a class which extends PreferenceActivity. This is an example on the preference class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| 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;
}
});
}
} |
We can call this activity when we click a button:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| @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 read these preferences from code, we should create a getPrefs() method, which we can call in the onStart() method. When we call it in the onStart() method instead of onCreate(), we can be sure that the preferences load when we have set them and returned to our main activity,
The getPrefs() method could look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| 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", "");
} |
Remember to add the following tag in your androidmanifest.xml file and add a new string item with the name "set_preferences" with the preference screen's title, for example "Preferences"
1
2
3
4
| <activity
android:name=".Preferences"
android:label="@string/set_preferences">
</activity> |
This is the final result
- The main activity screen
- The preference activity
No comments:
Post a Comment