main_activity.java
==================
package com.sam.web.sharedpreference;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private TextView detailsTextView;
private EditText usernameEditText,passwordEditText;
private Button saveButton,loadButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
detailsTextView=(TextView) findViewById(R.id.detailsTextViewId);
usernameEditText=(EditText) findViewById(R.id.usernameEditTextID);
passwordEditText=(EditText) findViewById(R.id.passwordEditTextID);
saveButton=(Button) findViewById(R.id.saveButtonId);
loadButton=(Button) findViewById(R.id.loadButtonId);
saveButton.setOnClickListener(this);
loadButton.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (view.getId()==R.id.saveButtonId){
String username=usernameEditText.getText().toString();
String password=passwordEditText.getText().toString();
//if username null and password null then toast show
if(username.equals("")&& password.equals(""))
{
Toast.makeText(getApplicationContext(),"please enter some data",Toast.LENGTH_SHORT).show();
}
else {
//writing data
SharedPreferences sharedPreferences=getSharedPreferences("userDetails", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putString("usernameKey","username");
editor.putString("passwordKey","password");
editor.commit();
Toast.makeText(getApplicationContext(),"data is store successfully",Toast.LENGTH_SHORT).show();
}
}
else if (view.getId()==R.id.loadButtonId){
// to read data
SharedPreferences sharedPreferences=getSharedPreferences("userDetails", Context.MODE_PRIVATE);
if (sharedPreferences.contains("usernameKey") && sharedPreferences.contains("passwordKey"))
{
String username=sharedPreferences.getString("usernameKey","Data not found");
String password=sharedPreferences.getString("passwordKey","Data not found");
detailsTextView.setText(username+" "+password);
}
}
}
}
==========================================================================================================
Activity_main.xml
=================
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.sam.web.sharedpreference.MainActivity">
<EditText
android:textStyle="bold"
android:textSize="20sp"
android:textAlignment="center"
android:hint="Edit your user name"
android:id="@+id/usernameEditTextID"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:inputType="textPassword"
android:textStyle="bold"
android:textSize="20sp"
android:textAlignment="center"
android:hint="Edit your user password"
android:id="@+id/passwordEditTextID"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginRight="10dp"
style="@style/Widget.AppCompat.Button.Colored"
android:textStyle="bold"
android:textSize="17sp"
android:text="save"
android:id="@+id/saveButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
style="@style/Widget.AppCompat.Button.Colored"
android:textStyle="bold"
android:textSize="17sp"
android:text="load"
android:id="@+id/loadButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_marginTop="20dp"
android:textAlignment="center"
android:textStyle="bold"
android:textSize="20sp"
android:id="@+id/detailsTextViewId"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>