How to get all keys of SharedPreferences programmatically in Android?

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

Solution 1:

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:

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:

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

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