Shared Preferences in Android
Shared Preferences in Android
In Android Programming, we use Shared Preferences to save temporary values to use in our Android App.
Value saved in shared preferences available for all activities of our app and availaible until we uninstall the app or clear data or manually delete the values.
Values like coins given to users after some task, or name or email of a user to show at some points in our app.
- Create Shared Preferences in Android App.
- Put values in Preferences
- Create a Set<String> to add list of values.
- Get values from Shared Preferences.
- Show it on a Text View.
- Use Debugger to see the values in Shared Preferences.
XML activity to create some Text Views to show values
XML activity
<?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=".SharedPrefs">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/email"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/subscribe"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/percentage"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/value"
android:padding="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/string_set"
android:padding="5dp"/>
</LinearLayout>
Then in Java file we will create Shared Preferences and save and get data from their.
Java Activity
package studio.harpreet.sampleproject;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SharedPrefs extends AppCompatActivity {
SharedPreferences preferences;
SharedPreferences.Editor editor;
String name;
String email;
Boolean subs;
int value;
float percentage;
Set<String> getDataset = new HashSet<>();
Set<String> dataset = new HashSet<>(); // almost same as arraylist
TextView name_tv,email_tv,sub_tv,perc_tv,int_tv,string_set;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared_prefs);
name_tv = findViewById(R.id.name);
email_tv = findViewById(R.id.email);
sub_tv = findViewById(R.id.subscribe);
perc_tv = findViewById(R.id.percentage);
string_set = findViewById(R.id.string_set);
int_tv = findViewById(R.id.value);
dataset.add("123");
dataset.add("456");
dataset.add("789");
//some values in shared preferences are from another activity, so don't be confuse about that
preferences = getSharedPreferences(getPackageName(),MODE_PRIVATE); // 1.) you can create many preferences with different id
editor = preferences.edit();
//adding values to preferences
editor.putString("name","Harpreet");
editor.putString("email","contact@harpreetstudio.com");
editor.putBoolean("subscribe",true);
editor.putFloat("percentage", (float) 99.9);
editor.putInt("value",1);
editor.putStringSet("stringset",dataset);
// after adding all values in editor, you should call editor.commit to add values in preferences
editor.apply();
editor.commit();
//get values from preferences
if(preferences != null) //if shared preferences exists
{
if(preferences.getString("name","") != null) // if string value exists
{
name = preferences.getString("name",""); //second string is only a default value we get
}
if(preferences.getString("email","") != null) // if string value exists
{
email = preferences.getString("email",""); //second string is only a default value we get
}
if(preferences.contains("subscribe")) // if preferences contains boolean value
{
subs = preferences.getBoolean("subscribe",false); //second string is only a default value we get
}
if(preferences.contains("value")) // if int value exists
{
value = preferences.getInt("value",0); //second string is only a default value we get
}
if(preferences.contains("percentage")) // if string value exists
{
percentage = preferences.getFloat("percentage",0); //second string is only a default value we get
}
if(preferences.contains("stringset")) // if string value exists
{
getDataset = preferences.getStringSet("stringset",null); //second string is only a default value we get
}
name_tv.setText(name);
sub_tv.setText(String.valueOf(subs));
email_tv.setText(String.valueOf(email));
int_tv.setText(String.valueOf(value));
perc_tv.setText(String.valueOf(percentage));
string_set.setText(String.valueOf(getDataset));
//you will get default values if value is null in shared preferences..
//to get values(elements) from string set
Iterator<String> it = getDataset.iterator();
int count = 0;
while (it.hasNext())
{
String value = it.next();
Log.e("TAG", "onCreate:count: "+count+" , "+value );
count++;
}
// now we will use debugger to see the values in shared preferences..
}
}
}
For manually removing the value you can useeditor.clear // to clear all values from preferencesoreditor.remove("name"); // to remove a specific value from preferences//don't forget to use these commands after changes in editor.editor.applyeditor.commit
Subscribe to Harpreet studio on Youtube
Like Harpreet Studio on Facebook
Follow me on Instagram
No comments