Hello
Today we are going to save any type of data using sharePreferances in android mobile.
Step-1
Make a new android app.
if you don't know how to do that then Read this post.
Step-2
Just drag and drop Edit Text and 2 Buttons into virtual mobile GUI as shown in figure.
Step-3
Just Copy and paste following code before onCreate method
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
now just copy and paste following code into onCreate method
sharedPref = MainActivity.this.getSharedPreferences("xyz", Context.MODE_PRIVATE);
editor=sharedPref.edit();
final EditText editText=(EditText) findViewById(R.id.editText);
Button btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editor.putString("city", String.valueOf(editText));
editor.commit();
}
});
Button btn2=(Button) findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s =sharedPref.getString("city", "mumbai");
Toast.makeText(MainActivity.this, String.valueOf(s), Toast.LENGTH_SHORT).show();
}
});
Now just run app into your mobile.
Today we are going to save any type of data using sharePreferances in android mobile.
Step-1
Make a new android app.
if you don't know how to do that then Read this post.
Step-2
Just drag and drop Edit Text and 2 Buttons into virtual mobile GUI as shown in figure.
Step-3
Just Copy and paste following code before onCreate method
SharedPreferences sharedPref;
SharedPreferences.Editor editor;
now just copy and paste following code into onCreate method
sharedPref = MainActivity.this.getSharedPreferences("xyz", Context.MODE_PRIVATE);
editor=sharedPref.edit();
final EditText editText=(EditText) findViewById(R.id.editText);
Button btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editor.putString("city", String.valueOf(editText));
editor.commit();
}
});
Button btn2=(Button) findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String s =sharedPref.getString("city", "mumbai");
Toast.makeText(MainActivity.this, String.valueOf(s), Toast.LENGTH_SHORT).show();
}
});
Now just run app into your mobile.
Comments
Post a Comment