87

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);
Niko
  • 8,093
  • 5
  • 49
  • 85
Piolo Opaw
  • 1,471
  • 2
  • 15
  • 21
  • 1
    Do you mean all keys of your preferences or the preference names? – Niko Feb 28 '14 at 08:05
  • my question is how to get all preferences that u decalred by name not the value of what inside of the preferences, because you can set the pref name if you want, so what if you create many preference names not a value inside the preference, problem is how to get all declared preference name. not the preference values inside the preference – Piolo Opaw Feb 28 '14 at 08:14

7 Answers7

205

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());
} 
Jose_GD
  • 2,279
  • 1
  • 21
  • 34
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • 4
    I asked google about this question and i was pointed to this answer right away, took my about 2.1sec to find the answer. Thank mate! – Sindri Þór Jan 27 '16 at 16:14
  • 1
    anyone knows why this has the potential to throw a NullPointerException? – Kushan Apr 08 '17 at 17:53
  • 2
    @kushan, map accepts null values. toString could cause a NPE and it is not necessary. The plus operator takes already care of the toString conversion – Blackbelt Apr 08 '17 at 18:39
19

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.

Hussein El Feky
  • 6,627
  • 5
  • 44
  • 57
Bhanu Sharma
  • 5,135
  • 2
  • 24
  • 49
  • The provided Google Code link doesn't seem to work. However, there is another library that is really good for extended logging: https://github.com/elvishew/xLog – Nah Mar 16 '21 at 18:13
15

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

Map<String, ?> map = sharedPreferences.getAll();
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
Mark Buikema
  • 2,483
  • 30
  • 53
8

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>
Morgan Koh
  • 2,297
  • 24
  • 24
5

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).

Hugo Allexis Cardona
  • 1,382
  • 19
  • 25
4

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());
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
2

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.

Peter F
  • 3,633
  • 3
  • 33
  • 45