How to use Android Preference

person Jason Huangfolder_openAndroid, Code, Javalocal_offer, , , access_time November 29, 2010

Android provided a class that makes it easy to implement your preference screen. In this example, I’m going to show you how to easily put in some preference/ settings options in your own android app.

1. You need to create an XML file (something called: userpreference.xml) in the /res/xml/ folder.

 

2. You simply need to create a new activity class that uses the xml file that defines the preferences above:
That is all you have to do, Android handles saving of the preference you set in the screen and persists on Application Context.

package monkeycancode.com.tipster;
 
import android.os.Bundle;
import android.preference.PreferenceActivity;
 
public class UserPreference extends PreferenceActivity {
 
		@Override
		public void onCreate(Bundle savedInstanceState){
			super.onCreate(savedInstanceState);
			addPreferencesFromResource(R.xml.userpreference);
		}
}

3. Now you need to read the stored preference values from Application Context and do something with it in your Application:
You need the import classes to for reading preference:

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;

This example shows how to simply read the string from preference stored in Application Context.

    private void GetPreference()
    {
    	Context context = getApplicationContext();
    	SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
 
    	String sDefaultTipPercent = prefs.getString(this.getString(R.string.pref_key_TipPercent), "15");
    	String sDefaultTaxPercent = prefs.getString(this.getString(R.string.pref_key_TaxPercent), "0");
    	String sDefaultExtraAmount = prefs.getString(this.getString(R.string.pref_key_ExtraAmount), "0");
    }

Comments

  1. Thanks, very good example, was searching for something like that quite some time.

  2. HI
    very thanks
    but i have a problem with R.String……
    this is my err :
    pref_key_TaxPercent cannot be resolved or is not a field

    i dont know how resolved this :

    srry for bad speak english 🙂

  3. It’s hard to find your articles in google. I found it on 17 spot, you should build quality backlinks , it will help you to increase traffic.
    I know how to help you, just search in google – k2 seo tips

  4. I see a lot of interesting posts on your page. You have to spend
    a lot of time writing, i know how to save you a lot of time, there is a tool that
    creates readable, SEO friendly articles in couple of seconds, just search in google –
    k2 unlimited content

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>