Wednesday, 20 March 2013

Shared Preferences Example in Android

activity_main.xml


    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="22dp"
            android:layout_marginTop="30dp"
            android:text="Enter Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <EditText
            android:id="@+id/pref_editText"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginTop="20dp"
            android:ems="10" />

        <TextView
            android:id="@+id/pref_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView2"
            android:layout_marginTop="28dp"
            android:layout_toRightOf="@+id/textView1"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
            android:id="@+id/show_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/pref_editText"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="28dp"
            android:layout_toRightOf="@+id/save_button"
            android:text="Show" />

        <Button
            android:id="@+id/save_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/show_button"
            android:layout_alignBottom="@+id/show_button"
            android:layout_alignLeft="@+id/pref_editText"
            android:text="Save" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/save_button"
            android:layout_below="@+id/show_button"
            android:layout_marginTop="105dp"
            android:text="Preference Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/textView2"
            android:layout_marginLeft="18dp"
            android:layout_toRightOf="@+id/textView2"
            android:text="Delete" />

        <Button
            android:id="@+id/clear_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/show_button"
            android:layout_centerVertical="true"
            android:text="Clear All" />

    </RelativeLayout>

MainActivity.java

package com.rajeshvijayakumar.sharedpreferences;

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

public class MainActivity extends Activity implements OnClickListener {

    private EditText mInputEditText;
    private TextView mOutputView;
    private Button mSaveButton;
    private Button mShowButton;
    private Button mDeleteButton;
    private Button mClearButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mInputEditText = (EditText) findViewById(R.id.pref_editText);
        mSaveButton = (Button) findViewById(R.id.save_button);
        mShowButton = (Button) findViewById(R.id.show_button);
        mDeleteButton = (Button) findViewById(R.id.delete_button);
        mOutputView = (TextView) findViewById(R.id.pref_textView);
        mClearButton = (Button) findViewById(R.id.clear_button);
        mSaveButton.setOnClickListener(this);
        mShowButton.setOnClickListener(this);
        mDeleteButton.setOnClickListener(this);
        mClearButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        String message="";
        switch (v.getId()) {
            case R.id.save_button:
                SavePreferences("PrefDemo", mInputEditText.getText().toString());
                message="Text Saved in Preferences";
                break;
            case R.id.delete_button:
                deletePreferences("PrefDemo");
                message = "Text Deleted from Preferences";
                break;
            case R.id.show_button:
                showPreferences("PrefDemo");
                message="Text Displayed from Preferences";
                break;
            case R.id.clear_button:
                clearAllPreferences();
                message="Removed All Text from All Preferences";
                break;
        }
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    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 deletePreferences(String key) {

        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(key);
        editor.commit();
    }
   
    private void clearAllPreferences() {
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }
    private void showPreferences(String key){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String savedPref = sharedPreferences.getString(key, "");
        mOutputView.setText(savedPref);
       }
}


output :








Source Code : Download this Example Here


9 comments:

  1. Very simple to understand

    ReplyDelete
  2. Super Tutorial, Very Simple Very Brilliant!

    ReplyDelete
  3. how to show all saved data in preferance

    ReplyDelete
  4. Hai buddy i want one help.I am new in android.I create one widget in my application it will be working fine for me,Suppose if i want to changes in my application and come back to home screen widget there is no updation in my home screen widget.I was asked question in stack overflow please refer my question link...
    http://stackoverflow.com/questions/18479557/android-application-listivew-widget-not-updating-automatically

    ReplyDelete
  5. Great tutorial but will you explain how to change layout color using shared preferences from a color picker
    Thank you in advance

    ReplyDelete
  6. hiiiiii.......... rajesh can u please send me the code for sending messeage particular time and date......

    ReplyDelete
  7. this is terrible, at last you upload source

    ReplyDelete
  8. thank you i can understand easily

    ReplyDelete