Friday, June 03, 2011

Shared Preferences Example

Shared Preferences used to share the data between two activities .In this example shows the how to create shared preferences.


First create a new project and then create two classes like following

Sharedpreferencesdemo.java:-

package com.SharedPreferencesDemo;
import android.app.Activity;
import android.content.Intent;
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 SharedPreferencesDemo extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    EditText edt;
    Button btn;
    TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //Refer objects from layout
        edt=(EditText)findViewById(R.id.edt);
        btn=(Button)findViewById(R.id.btn);
        tv=(TextView)findViewById(R.id.txt);
        //OnclickListener for Button
        btn.setOnClickListener(this);              
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
       
        // Use String Variable to store the data entered by the user
        String str=edt.getText().toString();
        //Create SharedPreference Called "SampleName With PrivateMode"
        SharedPreferences spf=getSharedPreferences("SampleName",0);       
        SharedPreferences.Editor editor=spf.edit();       
        editor.putString("Myname", str);
        editor.commit();
        //Notify the user with some message        
        Toast.makeText(this, "Inserted", Toast.LENGTH_LONG).show();          
        //Intent to call next activity
         Intent in=new Intent(this,ReadingSharedPreferences.class);          
         startActivity(in);
    }
}

And second one is

ReadingSharedPreferences.java:-


package com.SharedPreferencesDemo;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class ReadingSharedPreferences extends Activity{
   
      @Override
   public void  onCreate(Bundle savedInstanceState){                                                                                                                                                                       
            super.onCreate(savedInstanceState);    
            SharedPreferences  sp=this.getSharedPreferences("SampleName", 0);
             TextView tv=new TextView(this);           
             tv.setText(sp.getString("Myname", " "));
             setContentView(tv);
      }
}


After Then Create a XML file for Edit text box like

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"
    >
<EditText 
   android:id="@+id/edt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
   android:hint="Enter Your Name:"
    />
    <Button 
   android:id="@+id/btn"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="ClickMe"
    />
    <TextView
    android:id="@+id/txt"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
   
  </LinearLayout>


sec.xml:-


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
  <TextView
  android:id="@+id/txt"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
  <TextView
  android:id="@+id/txtone"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
 
</LinearLayout>



And finally the Android Manifest file is like this:-

AndroidManifest.xml:-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.SharedPreferencesDemo"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".SharedPreferencesDemo"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 <activity android:name=".ReadingSharedPreferences"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>


</manifest>




Like this way we can easily create shared preferences

1 comment: