I want to know how many shared preference in my shared preferences which are found in the file explorer by coding , is there any method to return the total number of shared preferences ?
Asked
Active
Viewed 6,204 times
9
-
14how many entries the sharedpreferences contain? sharedPreferencesInstance.getAll().size() – Blackbelt Jul 01 '13 at 10:04
-
1this is what I want to know by coding if possible – Akari Jul 01 '13 at 10:17
-
1I already give you a solution in my privious comment – Blackbelt Jul 01 '13 at 10:18
-
1@blackbelt: how to get the name of each pref plz ? – Akari Jul 01 '13 at 10:33
-
what does "name of each pref plz" mean ? – Blackbelt Jul 01 '13 at 10:35
-
the name of each xml file exists in shared_prefs folder – Akari Jul 01 '13 at 10:38
-
how many files do you create. getSharedPreferences("yourname") create a new file . prefs.putString("key", "value"); put samething inside the file named yourname. What do you want ? A list of all the entris inside yourname.xml? – Blackbelt Jul 01 '13 at 10:41
-
look at the picture which I've added to the post please .. I want to know the name of each entry in the shared preferences by coding – Akari Jul 01 '13 at 10:44
3 Answers
13
To get the number of entries you can use
sharedPreferencesInstance.getAll().size()
To retrieve all the keys you stored previously you can use keySet()
, as shown in the following snippet:
SharedPreferences prefs = this.getSharedPreferences("myshared", Context.MODE_PRIVATE);
Map<String,?> entries = prefs.getAll();
Set<String> keys = entries.keySet();
for (String key : keys) {
}

Blackbelt
- 156,034
- 29
- 297
- 305
8
In your activity try this:
SharedPreferences prefs = this.getSharedPreferences("your.package", Context.MODE_PRIVATE);
int howMany = prefs.getAll().size();

antoniom
- 3,143
- 1
- 37
- 53
2
Not sure if you are looking to something like this . If it is just the size then you can just do a
SharedPreferences pref = getSharedPreferences(<NAME>, <MODE>);
pref.getAll().size();

Community
- 1
- 1

Mark Pazon
- 6,167
- 2
- 34
- 50