In the following example, all setting keys are logged.
The key is that PreferenceGroup
is mixed in with the listed Preference
.
PreferenceScreen prefScreen = getPreferenceScreen();
int prefCount = prefScreen.getPreferenceCount();
for (int i = 0; i < prefCount; i++) {
Preference pref = prefScreen.getPreference(i);
if (pref instanceof PreferenceGroup) {
setupPrefsRecursive((PreferenceGroup)pref);
} else if (!Strings.isNullOrEmpty(pref.getKey())) {
Log.i(TAG, pref.getKey());
}
}
private void setupPrefsRecursive(PreferenceGroup prefGroup) {
int prefGroupCount = prefGroup.getPreferenceCount();
for (int i = 0; i < prefGroupCount; i++) {
Preference pref = prefGroup.getPreference(i);
if (pref instanceof PreferenceGroup) {
setupPrefsRecursive((PreferenceGroup)pref);
} else if (!Strings.isNullOrEmpty(pref.getKey())) {
Log.i(TAG, pref.getKey());
}
}
}
If you have the following settings
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mozc="http://schemas.android.com/apk/res-auto">
<PreferenceCategory android:title="@string/pref_cat_1">
<CheckBoxPreference
android:defaultValue="false"
android:key="pref_key_1"
android:title="@string/pref_title1" />
<PreferenceCategory android:title="@string/pref_cat_1_1">
<Preference
android:key="pref_key_2"
android:persistent="false"
android:title="@string/pref_title2">
</Preference>
</PreferenceCategory>
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_cat_2">
<CheckBoxPreference
android:defaultValue="true"
android:key="pref_key_3"
android:title="@string/pref_title3" />
</PreferenceCategory>
</PreferenceScreen>
The output will be as follows.
pref_key_1
pref_key_2
pref_key_3
I wrote it because there was nothing in particular.
Recommended Posts