Android provides many ways of storing app data such as an SQLiteDatabase, saving a data text file, etc. One of the ways is called SharedPreference. SharedPreferences allows you to save and require data in the form of keys and values and provides a simple method to read and write them.
The primary purpose of SharedPreference is to store user-specified configuration details, such as settings, and to keep the user logged in to the app. We can make use of SharedPreference in situations where we don’t need to store a lot of data and we don’t require any specific structure.
SharedPreference is used only when you need to save a small amount of data as a key, and value pair. Do not use SharedPreference to manage a larger amount of app data using other methods like an SQLiteDatabase.
SharedPreference uses two ways to save data. The first is with activity-based preferences and the second is creating custom preferences. In activity preferences, the users have to call function getPreference() for the activity class. In custom preferences, the users have to call the function **getSharedPreference(). **
In Android, we can create a new SharedPreference file or access an existing one by calling one of these methods:
>getSharedPreferences() - This method is used if you need multiple SharedPreference files identified by name, which you specify with the first parameter. This method calls from any context in your app.
>getPreferences() - This method is used only if you need one preference file for the activity. In this method, we don’t require a name as it will be the only preference file for this activity.
Write to SharedPreferences
To write to a SharedPreference file we can create a SharedPreferences.Editor by calling edit() on your sharedpreference.
>putInt() and putString() - These methods pass the keys and values.
>apply() and commit() - These methods save the changes. Use apply() method to write the updates to disk asynchronously and use commit() method to write the data to disk synchronously.
Read from SharedPreferences
To retrieve values from a SharedPreference file call these methods:
>getInt() and getString() - These methods provide the key for the value you want.
Example
activitymain.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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
<?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=".MainActivity"> <TextView android:id="@+id/tvWelcome" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:gravity="center_horizontal" android:text="Welcome message will be displayed here" android:textSize="18sp" android:textStyle="normal|bold" /> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:ems="10" android:gravity="center_horizontal|center_vertical" android:hint="please enter your name" android:inputType="textPersonName" /> <Button android:id="@+id/btnSubmit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="15dp" android:text="Submit" /> </LinearLayout> MainActivity.java package com.example.sharedpreferences; 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; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView tvWelcome; EditText etName; Button btnSubmit; public static final String MY_PREFS_FILENAME = "com.example.sharedpreferences.Names"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvWelcome = (TextView) findViewById(R.id.tvWelcome); etName = (EditText) findViewById(R.id.etName); btnSubmit = (Button) findViewById(R.id.btnSubmit); SharedPreferences preferences = getSharedPreferences(MY_PREFS_FILENAME, MODE_PRIVATE); String user = preferences.getString("user", ""); tvWelcome.setText("Welcome to my app " + user + "!"); btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name = etName.getText() .toString() .trim(); tvWelcome.setText("Welcome to my app " + name + "!"); SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_FILENAME, MODE_PRIVATE) .edit(); editor.putString("user", name); editor.commit(); } }); } }
Output: