Friday, August 4, 2017

Android application using shared preference.




/***************************************************************************************
Program Name: Write an android application using shared preference.
Author: sujith
Date: 25-10-2016
***************************************************************************************/


ActivityMain.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp"
tools:context="com.example.shamanthrai.sharedpreference.MainActivity"
>
<
TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shared Preference "
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="35dp"
/>
<
EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_marginTop="67dp"
android:hint="Name"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<
EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_below="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:paddingTop="20dp"
android:hint="Email"
/>
<
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/btnSave"
android:layout_below="@+id/editText3"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:onClick="Save"
/>
<
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get"
android:id="@+id/btnGet"
android:layout_toRightOf="@+id/btnSave"
android:layout_below="@id/editText3"
android:layout_marginTop="50dp"
android:layout_centerVertical="true"
android:onClick="Get"
android:layout_marginLeft="40dp"
/>
<
Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="@+id/btnClear"
android:layout_toRightOf="@+id/btnGet"
android:layout_below="@id/editText3"
android:layout_marginTop="50dp"
android:layout_marginLeft="30dp"
android:onClick="Clear"
/>
</
RelativeLayout>




MainActivity.java:

package com.example.shamanthrai.sharedpreference;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
SharedPreferences
sharedpreferences;
TextView
name,email;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
public static final String Email = "emailKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.
activity_main);
name = (TextView) findViewById(R.id.editText);
email = (TextView) findViewById(R.id.editText3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
if (sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
if (sharedpreferences.contains(Email)) {
email.setText(sharedpreferences.getString(Email, ""));
}
}
public void Save(View view){
String n=
name.getText().toString();
String e=
email.getText().toString();
Editor editor=
sharedpreferences.edit();
editor.putString(
Name,n);
editor.putString(
Email,e);
editor.commit();
Toast.
makeText(MainActivity.this,"Saved!!", Toast.LENGTH_LONG).show();
}
public void Clear(View view)
{
name=(TextView)findViewById(R.id.editText);
email=(TextView)findViewById(R.id.editText3);
name.setText("");
email.setText("");
}
public void Get(View view){
name=(TextView)findViewById(R.id.editText);
email=(TextView)findViewById(R.id.editText3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

if (sharedpreferences.contains(Name)) {
name.setText(sharedpreferences.getString(Name, ""));
}
if (sharedpreferences.contains(Email)) {
email.setText(sharedpreferences.getString(Email, ""));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
}




OUTPUT: