Pr.Pg Next Pg

CheckBoxPreference tutorials

  • Shows an item with a checkbox for a setting that is either enabled or disabled. The saved value is a boolean (true if it's checked).

ListPreference

  • Opens a dialog with a list of radio buttons. The saved value can be any one of the supported value types (listed above).

EditTextPreference

  • Opens a dialog with an EditText widget. The saved value is a String.

  • We define various attributes for each of them like:

android:key

  • This attribute is required for preferences that persist a data value. It specifies the unique key (a string) the system uses when saving this setting's value in the SharedPreferences.

  • The only instances in which this attribute is not required is when the preference is a PreferenceCategoryor PreferenceScreen, or the preference specifies an Intent to invoke (with an <intent> element) or aFragment to display (with an android:fragment attribute).

android:title

  • This provides a user-visible name for the setting.

android:summary

  • The summary for the Preference in a PreferenceActivity screen.

  • We also have to set up an XML file in our values folder in our res folder.

  • The name of the file should be the same used in the android:entries and android:entryValues attributes.

  • Hence in this case we make an XML file named array.

  • The code within the file should be as follows.

 

array.xml

 

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

<string-array name="list">

<item>Option No 1</item>

<item>Option No 2</item>

<item>Option No 3</item>

<item>Option No 4</item>

</string-array>

<string-array

name="listValues">

<item>A</item>

<item>B</item>

<item>C</item>

<item>D</item>

 

</string-array>

 

</resources>

 

  • Here we define two string arrays.

  • One named list where we stored the name of the entries in the ListPreference.

  • In the other array named listValues we store the value corresponding to these entries.

  • We now define the attributes used above

  • android:entries

  • The human-readable array to present as a list.

  • android:entryValues

  • The array to find the value to save for a preference when an entry from entries is selected. 

  • So now it will be easy understand what the items in the array are.

  • Make sure that the name of the arrays matches with the path name defined in those attributes.

  • We now have to create an activity class that will handle our preferences.

  • We do that by creating a new class named Prefs.java.

Below is the code that will add the XML layout of our preferences to our activity.

 

 

Prefs.java

 

package com.example.helloandroid;

 

import android.app.Activity;

import android.os.Bundle;

import android.preference.PreferenceFragment;

 

public class Prefs extends Activity{

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 

// Display the fragment as the main content.

getFragmentManager().beginTransaction().replace(android.R.id.content,

new PrefsFragment()).commit();

}

 

 

public static class PrefsFragment extends PreferenceFragment {

 

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

 

// Load the preferences from an XML resource

addPreferencesFromResource(R.xml.prefs);

}

}

}

 

 

  • We have a class named Prefs which extends Activity.

  • Inside the onCreate() method of that activity, we set the fragment to be the main content to be displayed.

  • A fragment basically represents a behaviour or a portion of the user interface.

  • Since we have only one fragment in this activity we set it as the main content.

  • Next we also define a nested class named PrefsFragment which extends PreferenceFragment.

  • Inside the onCreate() method of this class we use the addPreferenceFromResources() method to load the Preferences from the XML resource file.

  • This method will work for API higher than 10.

  • For API lower than or equal to 10 you can use the PreferenceActivity.

 

public classSettingsActivityextendsPreferenceActivity

{
@Override
public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

  • The method addPreferenceFromResources() is deprecated for PreferenceActivity for API higher than 10.

 

Running the Application

  • When we run the application we can see the preference Option and on clicking that we see the following screens.

Accessing our Preferences

  • We will now show how to access the preferences from any activity.

  • We will add the following statements in the onCreate() method of our MainAcitiy.java file.

 

 

 

SharedPreferences getPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

if(getPreferences.getBoolean("check_box", true)){

song.start();

}

  • We create an object of the SharedPrefences in which store the default shared preferences and apply them for out current activity using the getBaseContext() method which we pass in the parameter.

  • We then access our CheckBox preference using its key and if it is checked only then we will play the music which we had added, otherwise we wont play.

  • The parameter true in the getBoolean() method is the defaultValue in case the preference does not exist.

  • Now if we want to access our EditText and List preferences we will demonstrate it with an example.

  • In this example we will change the text of our TextView in the MainActivity from Hello Android!!! to whatever we have typed in our EditText preferences only when we have set the List preference with option no 1.

  • The following code should be added in the onCreate() method.

 

 

 

String text = getPreferences.getString("entered_text", "Hello Android!!!");

String value = getPreferences.getString("list", "C");

if(value.contentEquals("A")){

display.setText(text);

}

 

Pr.Pg border                                              Next Pg