activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:gravity="center"
android:orientation="vertical"
tools:context="com.androidsimplifycodes.example.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<EditText
android:id="@+id/edText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Save" />
</LinearLayout>
<TextView
android:id="@+id/tvStoredData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:textAppearanceLarge"
android:textColor="@color/colorAccent" />
</LinearLayout>
MainActivity.java
package com.androidsimplifycodes.example;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
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 {
protected Context context;
protected SharedPreferences sharedPreferences;
protected SharedPreferences.Editor editor;
protected EditText edText;
protected TextView tvStoredData;
protected Button btnSave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
context = MainActivity.this;
initView();
// sharedPreferences used for the storing the data
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
// get stored data from the SharedPreferences for the TAG _DATA
tvStoredData.setText(sharedPreferences.getString("_DATA", "Default String"));
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editor = sharedPreferences.edit();
editor.putString("_DATA", edText.getText().toString());
// commit the changes
boolean result = editor.commit();
// if data committed then return true otherwise return false
if (result) {
Toast.makeText(context, "Data Stored into SharedPreferences", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Problem Arrive for the storing Data", Toast.LENGTH_SHORT).show();
}
}
});
}
private void initView() {
edText = (EditText) findViewById(R.id.edText);
tvStoredData = (TextView) findViewById(R.id.tvStoredData);
btnSave = (Button) findViewById(R.id.btnSave);
}
}
Output :
Here, sharedPreferences _DATA key not have any value so default string return.
String stored into sharedPreferences if successfully then return result true else false. Note : Only commit() function return result so we can use commit() function instead of apply().
Now _DATA key have some value so data will be return by sharedPreferences.