Welcome folks today in this blog post we will be reading,storing & deleting
data in android app using SharedPreferences
in java. All the full source code of the application is shown below.
Get Started
In order to get started you need to make a new android
project inside the android studio and then you will see the below directory
structure as shown below at the end
of the project.
Now we need to edit the activity_main.xml
where we have the simple user
form where we have two editText
fields where the user can enter the name
and the age and also we also have the delete
button to delete the data from the sharedPreferences
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?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" tools:context=".MainActivity" tools:ignore="HardcodedText"> <TextView android:id="@+id/textview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="32dp" android:text="Shared Preferences Demo" android:textColor="@android:color/black" android:textSize="24sp" /> <!--EditText to take the data from the user and save the data in SharedPreferences--> <EditText android:id="@+id/edit1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/textview" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:layout_marginEnd="16dp" android:hint="Enter your Name" android:padding="10dp" /> <!--EditText to take the data from the user and save the data in SharedPreferences--> <EditText android:id="@+id/edit2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/edit1" android:layout_marginStart="16dp" android:layout_marginTop="8dp" android:layout_marginEnd="16dp" android:hint="Enter your Age" android:inputType="number" android:padding="10dp" /> <Button android:id="@+id/delete_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/edit2" android:layout_alignParentEnd="true" android:layout_marginTop="90dp" android:layout_marginEnd="154dp" android:text="Delete Data" /> </RelativeLayout> |
And now we need to edit the MainActivity.java
file and copy paste the following code
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.example.downloadbulkproject; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private EditText name, age; private Button delete_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = findViewById(R.id.edit1); age = findViewById(R.id.edit2); delete_btn = findViewById(R.id.delete_btn); } } |
As you can see in the above java
code we are getting the references of the two editText
fields and the delete
button which is the name
and the age
of the person. And now we need to save this data
so that when we close
the app and reopens the app the data
stays inside the app.
Storing Data in SharedPreferences
Now we need to implement the lifecycle
method which is onPause()
where we will be storing the data entered by the user inside the SharedPreferences
which will be the key value pair as shown below
1 2 3 4 5 6 7 8 9 10 11 12 |
@Override protected void onPause() { super.onPause(); // Creating a shared pref object with a file name "MySharedPref" in private mode SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref", MODE_PRIVATE); SharedPreferences.Editor myEdit = sharedPreferences.edit(); // write all the data entered by the user in SharedPreference and apply myEdit.putString("name", name.getText().toString()); myEdit.putInt("age", Integer.parseInt(age.getText().toString())); myEdit.apply(); } |
As you can see in the above java
code we are make a new object
of the SharedPreferences
class and inside it we are provide the name
of the data and then the mode
which is private means that this app data can only be used by this app. And then we are inserting
some key value pairs such as for the name and the age inside the sharedPreferences
object. For this we are using the putString()
method for strings and putInt()
for integers.
Reading Data From SharedPreferences
Now we need to implement the lifecycle
method which is onResume()
where we will be retrieving the stored data in the SharedPreferences
which will be the key value pair for the name
and the age as shown below.
1 2 3 4 5 6 7 8 9 10 11 12 |
@Override protected void onResume() { super.onResume(); // Fetching the stored data from the SharedPreference SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_PRIVATE); String s1 = sh.getString("name", ""); int a = sh.getInt("age", 0); // Setting the fetched data in the EditTexts name.setText(s1); age.setText(String.valueOf(a)); } |
Deleting SharedPreferences Data
Now we will be looking on how we can remove the data
stored inside the sharedPreferences
object for this we will be using the remove()
method to pass the string key
to remove the data and lastly we are calling the apply()
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
delete_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE); SharedPreferences.Editor myEdit = sharedPreferences.edit(); myEdit.remove("name"); myEdit.remove("age"); name.setText(""); age.setText(String.valueOf(0)); myEdit.apply(); } }); |
FULL SOURCE CODE
Wrapping the blog post this is the full source code of the MainActivity.java
file as shown below
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
package com.example.downloadbulkproject; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { private EditText name, age; private Button delete_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = findViewById(R.id.edit1); age = findViewById(R.id.edit2); delete_btn = findViewById(R.id.delete_btn); delete_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref",MODE_PRIVATE); SharedPreferences.Editor myEdit = sharedPreferences.edit(); myEdit.remove("name"); myEdit.remove("age"); name.setText(""); age.setText(String.valueOf(0)); myEdit.apply(); } }); } // Fetch the stored data in onResume() Because this is what will be called when the app opens again @Override protected void onResume() { super.onResume(); // Fetching the stored data from the SharedPreference SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_PRIVATE); String s1 = sh.getString("name", ""); int a = sh.getInt("age", 0); // Setting the fetched data in the EditTexts name.setText(s1); age.setText(String.valueOf(a)); } // Store the data in the SharedPreference in the onPause() method // When the user closes the application onPause() will be called and data will be stored @Override protected void onPause() { super.onPause(); // Creating a shared pref object with a file name "MySharedPref" in private mode SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref", MODE_PRIVATE); SharedPreferences.Editor myEdit = sharedPreferences.edit(); // write all the data entered by the user in SharedPreference and apply myEdit.putString("name", name.getText().toString()); myEdit.putInt("age", Integer.parseInt(age.getText().toString())); myEdit.apply(); } } |