[JAVA] [Android] List all setting items on the setting screen

environment

code

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

reference

I wrote it because there was nothing in particular.

Recommended Posts

[Android] List all setting items on the setting screen
[Android] Get the tapped position (coordinates) on the screen
Display the list in setDetails on the screen with spring-security
Customize list view on Android
Delete all the contents of the list page [Ruby on Rails]
[Android] Get the date on Monday
How to display products by category on the same list screen
Try using the service on Android Oreo
Display an image on the base64 screen
How the website is displayed on the screen
[No.004] Corrected the order list screen of the orderer
[Java] 4 steps to implement splash screen on Android
[No.003] Create an order list screen for the orderer
[Android] Solution when the camera cannot be started on Android 9
I saw the list view of Android development collectively
Import the instance and use it on another screen
Setting up the FreeBSD desktop environment on Ubuntu + qemu
<Android> Change the background color of the List row of ListView
[Android] Display images and characters on the ViewPager tab
[Android Studio] [Java] How to fix the screen vertically
Run the Android emulator on Docker using Android Emulator Container Scripts
[Android] How to get the setting language of the terminal
[Introduction] Display Android Studio Hello World on the emulator
I want to simplify the log output on Android
Until the Google Assistant sample runs on Android Things