Wednesday, April 20, 2011

Using SharedPrefrenced in Android?



Step1: create a project as Usual

Step2: create the xml and code like this


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cropToPadding="true"
android:background="@drawable/mylog"
android:adjustViewBounds="true"

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

>
<TextView
android:text="User Name:"
android:textColor="#AF8717"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:textSize="22sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="45dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/myuserlabel"
/>

<EditText
android:layout_toRightOf="@+id/myuserlabel"
android:textColor="#AF8717"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:textSize="22sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="40dp"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/userval"
android:ellipsize="end"
/>


<TextView
android:layout_below="@+id/myuserlabel"
android:text="Password:"
android:textColor="#AF8717"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:textSize="22sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"
android:layout_marginTop="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mypwdlabel"
/>

<EditText
android:layout_toRightOf="@+id/mypwdlabel"
android:layout_marginTop="130dp"
android:textColor="#AF8717"
android:layout_weight="1"
android:textSize="22sp"
android:textStyle="bold"
android:password="true"
android:layout_marginLeft="35dp"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:id="@+id/mypwdval"
android:ellipsize="end"


/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/submit"
android:layout_below="@+id/mypwdlabel"
android:layout_marginTop="80dp"
android:layout_centerInParent="true"


/>


</RelativeLayout>

Step 3 :Java code


public class DemoActivity extends Activity
{
EditText u_name=null,pwd=null;
Button mylogin_submit;

/* Creation of Shared Preferences */

public static final String PREFS_NAME = "MyShardedprefernce";
static SharedPreferences settings;
SharedPreferences.Editor editor;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.main);
u_name=(EditText)findViewById(R.id.userval);
pwd=(EditText)findViewById(R.id.mypwdval);
mylogin_submit = (Button)findViewById(R.id.submit);

mylogin_submit.setOnTouchListener(new OnTouchListener (){


@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
username=u_name.getText().toString();
Log.d("UserName",username);
password=pwd.getText().toString();

//store it in shared perfernce

editor=settings.edit();
editor.putString("UserName",username);
editor.putString("Password",password);
Intent myintent=new Intent(DemoActivity.this,NewActivity.class);
startActivity(myintent);

return false;
}});


}


Step 4: Create another class , where we are reterving the value stored in Shared Prefernce


public class NewActivity extends Activity {

public static final String PREFS_NAME = "MyShardedprefernce";
static SharedPreferences settings;
String username, password;
TextView t,t1;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.newui);
settings = getSharedPreferences(PREFS_NAME, 0);

username= settings.getString("UserName","No Data Found");
password= settings.getString("Password","No Data Found");

t=(TextView) findViewById(R.id.Username);
t1=(TextView) findViewById(R.id.pwd);

t.SetText("Welcome User:"+username);
}
}

No comments:

Post a Comment

Notification in Android Using AlarmManager, BoradCastReceiver & Services

    Our aim is to show notification message to user in a specific time.     Say For example , I have planned to show Notification ...