Monday, January 24, 2011

Example of using SharedPreferences.Editor

Example of using SharedPreferences.Editor

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Saved Mem 1:"
/>
<TextView
android:id="@+id/savedmem1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Saved Mem 2:"
/>
<TextView
android:id="@+id/savedmem2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/save_mem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Mem 1"
/>
<EditText
android:id="@+id/edittext2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/save_mem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save Mem 2"
/>
</LinearLayout>


AndroidSharedPreferencesEditor.java
package com.exercise.AndroidSharedPreferencesEditor;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidSharedPreferencesEditor extends Activity {

EditText editText1, editText2;
TextView textSavedMem1, textSavedMem2;
Button buttonSaveMem1, buttonSaveMem2;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textSavedMem1 = (TextView)findViewById(R.id.savedmem1);
textSavedMem2 = (TextView)findViewById(R.id.savedmem2);
editText1 = (EditText)findViewById(R.id.edittext1);
editText2 = (EditText)findViewById(R.id.edittext2);
buttonSaveMem1 = (Button)findViewById(R.id.save_mem1);
buttonSaveMem2 = (Button)findViewById(R.id.save_mem2);

buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener);
buttonSaveMem2.setOnClickListener(buttonSaveMem2OnClickListener);

LoadPreferences();
}

Button.OnClickListener buttonSaveMem1OnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SavePreferences("MEM1", editText1.getText().toString());
LoadPreferences();
}

};

Button.OnClickListener buttonSaveMem2OnClickListener
= new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SavePreferences("MEM2", editText2.getText().toString());
LoadPreferences();
}

};

private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}

private void LoadPreferences(){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String strSavedMem1 = sharedPreferences.getString("MEM1", "");
String strSavedMem2 = sharedPreferences.getString("MEM2", "");
textSavedMem1.setText(strSavedMem1);
textSavedMem2.setText(strSavedMem2);
}
}


Download the files.

Related Article:
- Use getSharedPreferences() to retrieve a preferences object to shared across multiple activity

9 comments:

lookingforgay said...
This comment has been removed by the author.
Android Er said...

hello lookingforgay,

I can't catch your question! Even in SurfaceView, you can use SharedPreferences also.

Firzan.... said...

hi...
i have ,saved the shared preferences in 1 activity...
and now i want to retrieve those shared preferences on service .how i do that???
plz reply

Android Er said...

Hello Firzan....

get SharedPreferences object using the method getPreferences(), allow accessing preferences associated with the activity only. To use preferences that are shared across multiple application components (activities, receivers, services, providers), you can use the Context.getSharedPreferences() method to retrieve a preferences object.

Refer Use getSharedPreferences() to retrieve a preferences object shared across multiple activity.

Aaron said...

Thanks for the tutorial. I am writing an log in screen and the tutorial helped me with the checkbox to remember the username and password.

Apoorv said...

Hi, Thanks for this tutorial. It is very clear. But I have one doubt. Can I use this sharedpreferences to get the values in the edittext and so that I can display the same values in the respective edittexts after switching the mode from portrait to landscape. Please reply me.

Andr.oid Eric said...

hello Apoorv,

Sure you can.

But - in my understanding, EditText (with ID assigned) will keep content after orientation changed. No need special handle for it. Refer EditText keep no change after orientation changed.

Anonymous said...

WIll this save so when I close the application (force quit it so it isn't running at all) and I can still pull up the values from sharedpreferences?

Andr.oid Eric said...

yes, it can.