Anshuman Kaushik Anshuman Kaushik - 1 year ago 73
Java Question

How to get keys as well as values from sharedPreference file

Whenever I try to get key and values from my sharedPreference file, I get only values like I have a key name "Anshuman" whose value is "Present".

But whenever I try my code I got only "Present". How can I get "Anshuman" too.

protected void onCreate(Bundle savedInstanceState) {

SharedPreferences sharedPreferences = getSharedPreferences("Date", MODE_PRIVATE);
Map<String,?> keys = sharedPreferences.getAll();

for(Map.Entry<String,?> entry : keys.entrySet()){


Answer Source

You can test this code :

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

enter link description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download