Wednesday, November 21, 2012

Shared Preference in Android

In this tutorial we learn about how to use Shared Preference in android.


Simple Demo example of shared prefrence.
In the below example main activity xml look like this.
<?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"
   >
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       >
        <requestFocus />
    </EditText>
    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        
         />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Save" />

    <Button
        android:id="@+id/showdata"
        android:layout_marginTop="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Data" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />
     <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>


JAVA file for save data in shared preference.

private Button save,show;
 private EditText edt1,edt2;
 private TextView txt1,txt2;
 private String str1,str2;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.abcd);

  save  =  (Button) findViewById(R.id.button1);
  show  =  (Button) findViewById(R.id.showdata);
  edt1 = (EditText) findViewById(R.id.editText1);
  edt2 = (EditText) findViewById(R.id.editText2);
  txt1 = (TextView) findViewById(R.id.textView1);
  txt2 = (TextView) findViewById(R.id.textView2);
  
  save.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    
    str1 = edt1.getText().toString();
    str2 = edt2.getText().toString();
    
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
       SharedPreferences.Editor editor = sharedPreferences.edit();
       editor.putString("EDT1", str1);
       editor.putString("EDT2", str2);
       editor.commit();
   }
  });
  
  show.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {

    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
       String strSavedEDT1 = sharedPreferences.getString("EDT1", "");
       String strSavedEDT2 = sharedPreferences.getString("EDT2", "");
       txt1.setText(strSavedEDT1);
       txt2.setText(strSavedEDT2);
   }
  });
  
 } 
In above example on save button click value save to shared preference.

No comments:

Post a Comment