Using shared preferences in android

Storing app data is an essential requirement of most applications. Android lets your store data using Shared Preferences and Sqlite database. You can use either of them, based on your requirement. Check this post to decide which one suits your requirement

Shared Preferences  stores data as key-pairs only. Key is required to read data from it. Reading amd storing data in it is very simple .But, it’s difficult to store and read large structured data . It saves  primitives data type like string, int ,booleans, floats ,long etc. It is best to use Shared Preferences when only small amount of data needs to be stored eg few app settings, user login /password etc.

Step 1: Create your shared preferences in the following way :

SharedPreferences myPreferences = this.getSharedPreferences("myPreferences", 0);

Step 2: Save values in shared preferences using editor.
“myInt” is the key and “2” is the value to be stored

SharedPreferences.Editor prefEditor = myPreferences.edit();
		prefEditor.putInt("myInt", 2);
		prefEditor.commit();

Step 3: Retrieve values from shared preferences using the key. Second parameter of getInt(“myInt”, 0) denotes the default value to be returned if no value is saved yet.

myPreferences.getInt("myInt", 0);

Complete Example:

public class SharedPreferenceActivity extends Activity implements
		OnClickListener {
	SharedPreferences myPreferences;
	EditText nameEditText, AgeEditText;
	Button saveBtn, showBtn;
	TextView showTextview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		nameEditText = (EditText) findViewById(R.id.nameEdittext_id);
		AgeEditText = (EditText) findViewById(R.id.ageEdittext_id);
		saveBtn = (Button) findViewById(R.id.saveBtn_id);
		showBtn = (Button) findViewById(R.id.showBtn_id);
		showTextview = (TextView) findViewById(R.id.showText_id);
		// creating "myPreferences" sharedPreference
		myPreferences = this.getSharedPreferences("myPreferences", 0);

		saveBtn.setOnClickListener(this);
		showBtn.setOnClickListener(this);

	}

	void setIntAppPref(int a) {
		SharedPreferences.Editor prefEditor = myPreferences.edit();
		prefEditor.putInt("myInt", a);
		prefEditor.commit();
	}

	int getIntAppPref() {
		return myPreferences.getInt("myInt", 0);
	}

	void setStringAppPref(String a) {
		SharedPreferences.Editor prefEditor = myPreferences.edit();
		prefEditor.putString("myString", a);
		prefEditor.commit();
	}

	String getStringAppPref() {
		return myPreferences.getString("myString", "");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.saveBtn_id:
			setStringAppPref(nameEditText.getText().toString());
			setIntAppPref(Integer.parseInt(AgeEditText.getText().toString()));

			break;
		case R.id.showBtn_id:
			showTextview.setText("Name:" + getStringAppPref() + '\n' + "Age:"
					+ getIntAppPref());

			break;

		default:
			break;
		}

	}

}

Screenshots:

pref1              pref2

Download the example code here

download_image.

Related Posts:

Difference between sqlite and shared preferences in android

PreferenceActivity in Android

 

Advertisements

Difference between sqlite and shared preferences in android

Storing app data is an essential requirement of most applications. Android lets your store data using Shared Preferences and Sqlite database. You can use either of them, based on your requirement.

Shared Preferences

Shared Preferences stores data as key-pairs only. Key is required to read data from it. Reading amd storing data in it is very simple .But, it’s difficult to store and read large structured data . It saves  primitives data type like string, int ,booleans, floats ,long etc. It is best to use Shared Preferences when only small amount of data needs to be stored eg few app settings, user login /password etc.

Sqlite Database

It stores structured data as a database. The data can be queried and hence ,this makes it possible to search database. Reading data from sqlite database is slower and more expensive then shared preferences. SQLite database is useful for just about anything and very flexible. It is best to use Sqlite when the data to be stored is large , structured and required searching .eg storing complete user details , storing data fetched by http request etc.

Related Posts:

PreferenceActivity in Android

Using shared preferences in android