How to get all keys of SharedPreferences programmatically in Android?

AndroidAndroid Preferences

Android Problem Overview


How to get all keys in SharedPreferences, not the value of the preference just key only?

prefA = getSharedPreferences("MyAttack", MODE_PRIVATE);
prefB= getSharedPreferences("MySkill", MODE_PRIVATE);

Android Solutions


Solution 1 - Android

SharedPreferences has the method getAll() that returns a Map<String, ?> . From the Map you can retrieve easily the keys with keySet() and the key/value mappings with entrySet():

Map<String, ?> allEntries = prefA.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
    Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());
} 

Solution 2 - Android

What you can do is use getAll() method of SharedPreferences and get all the values in Map and then you can easily iterate through them:

Map<String,?> keys = prefs.getAll();

for(Map.Entry<String,?> entry : keys.entrySet()){
    Log.d("map values",entry.getKey() + ": " + entry.getValue().toString());            
}

For more, you can check PrefUtil.java's dump() implementation with this link.

Solution 3 - Android

Use the getAll() method of android.content.SharedPreferences.

Map<String, ?> map = sharedPreferences.getAll();

Solution 4 - Android

Kotlin will allow you to get all your SharedPreferences keys with just one line by using Map.

Cheers mate 

val sharedPreferences = context.getSharedPreferences("SHARED_PREFERENCES", Context.MODE_PRIVATE)
val sharedPreferenceIds = sharedPreferences.all.map { it.key } //returns List<String>

Solution 5 - Android

Check out the below code for getAll() method

Map<String, ?> prefsMap = prefA.getAll();
for (Map.Entry<String, ?> entry: prefsMap.entrySet()) {
    Log.v("SharedPreferences", entry.getKey() + ":" + 
entry.getValue().toString());
}

Solution 6 - Android

After reading @Delacrix response and playing with the Kotlin-way (tested in Kotlin 1.3.11) of retrieving the keys, I found out an even shorter version for getting the keys (or even the values):

val prefsA = context.getSharedPreferences("MyAttack", Context.MODE_PRIVATE)
val prefsAIDs = sharedPreferences.all.keys //returns MutableSet<String>

The same way, you can access only the values via sharedPreferences.all.values (even tho is not what you asked in your question, might be a useful for other readers).

Solution 7 - Android

Although @Blackbelt's answer is quite popular here, I think it is not actually targeting the question. (It is not suprising since the question mixes up the terminology of preferences names and keys.) I guess the question is how to find out which shared preferences instances have been created - which can be of interest if the names are created dynamically.

Here are two strategies for that:

  • create another shared preferences "meta" instance where all created shared prefences instances are registered by adding a key/value pair for it to the meta prefs - with the key being the shared prefences name and the value being any value e.g. true.

     getSharedPreferences( DYNAMIC_PREFS_NAME, 0 )
         .edit().put*(*).apply();
     getSharedPreferences( "meta_prefs_index", 0 )
         .edit().putBoolean( DYNAMIC_PREFS_NAME, true).apply();
    

To obtain all shared prefences created by you, use the meta prefs and follow the answer of @Blackbelt.

  • shared preferences have a backup file, which is stored in folder /data/data/YOUR_PACKAGE_NAME/shared_prefs with name YOUR_PREFS_NAME.xml So you can look into that directory for your shared preferences files. But be careful, there might be shared preferences files that were not created by your logic! Therfore I would stick with the first approach.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPiolo OpawView Question on Stackoverflow
Solution 1 - AndroidBlackbeltView Answer on Stackoverflow
Solution 2 - AndroidBhanu SharmaView Answer on Stackoverflow
Solution 3 - AndroidMark BuikemaView Answer on Stackoverflow
Solution 4 - AndroidMorgan KohView Answer on Stackoverflow
Solution 5 - AndroidNaveed AhmadView Answer on Stackoverflow
Solution 6 - AndroidHugo Allexis CardonaView Answer on Stackoverflow
Solution 7 - AndroidPeter FView Answer on Stackoverflow