Android Programming

Hands On No. 12 : Working with Preference Storage

Resources

Source Code of MainAcivity.java
Sharedpreferences sharedpreferences;
String mypreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button save = (Button) findViewById(R.id.save1);
Button display = (Button) findViewById(R.id.display1);
final EditText et1 = (EditText) findViewById(R.id.txtname1);
sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE);

save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

String n = et1.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(uname, n);
editor.commit();
Toast.makeText(getBaseContext(), "Data is saved successfully!",
Toast.LENGTH_SHORT).show();


}
});
display.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {

sharedpreferences = getSharedPreferences(mypreference,Context.MODE_PRIVATE);

if (sharedpreferences.contains(uname)) {

String t = sharedpreferences.getString(uname,"");
Toast.makeText(getBaseContext(), "Data is \n"+t,Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "No Content Found",
Toast.LENGTH_SHORT).show();
}
}
});
}
Source Code of activity_main.xml
<?xml version="1.0" encoding="utf-8"?>


<ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent" android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Enter Username"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:layout_marginTop="20dp"
android:textSize="25sp"
android:textColor="@android:color/holo_blue_light" />

<EditText
android:id="@+id/txtname1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="20"
android:hint="Enter Name"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp"
android:inputType="textPersonName"
/>

<Button
android:id="@+id/save1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="10dp"
android:background="@color/colorAccent"
android:text="SAVE"
android:textColor="@android:color/white"
android:textSize="25sp" />
<Button
android:id="@+id/display1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:layout_marginRight="40dp"
android:layout_marginBottom="70dp"
android:background="@color/colorAccent"
android:text="DISPLAY"
android:textColor="@android:color/white"
android:textSize="25sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Developed By : w3techblog.com"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:layout_marginTop="20dp"
android:textSize="25sp"
android:textColor="@android:color/holo_blue_light" />
</LinearLayout>
</ScrollView>