Java Code Examples for androidx.preference.PreferenceScreen#getPreferenceCount()

The following examples show how to use androidx.preference.PreferenceScreen#getPreferenceCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SettingsFragment.java    From android-popular-movies-app with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

    // Add movie preferences, defined in the XML file in res->xml->pref_movie
    addPreferencesFromResource(R.xml.pref_movie);

    // Get shared preferences
    SharedPreferences sharedPreferences = getPreferenceScreen().getSharedPreferences();
    // Get the preference screen
    PreferenceScreen prefScreen = getPreferenceScreen();
    // Get the number of preferences
    int count = prefScreen.getPreferenceCount();

    // Go through all of the preferences, and set up their preference summary.
    for (int i = 0; i < count; i++) {
        Preference p = prefScreen.getPreference(i);

        String value = sharedPreferences.getString(p.getKey(), DEFAULT_VALUE);
        setPreferenceSummary(p, value);
    }
}
 
Example 2
Source File: EnabledApplicationsPreferenceFragment.java    From an2linuxclient with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
    boolean newSettingsValue = sp.getBoolean(key, false);
    String prefEnableDisableAllKey = getString(R.string.preference_enable_disable_all_applications);
    if (key.equals(prefEnableDisableAllKey)) {
        final PreferenceScreen prefScreen = findPreference(getString(R.string.enabled_apps_pref_screen));
        for (int i = 1; i < prefScreen.getPreferenceCount(); i++) {
            ((CheckBoxPreference) (prefScreen.getPreference(i))).setChecked(newSettingsValue);
        }
    }

    // If newSettingsValue == false, clear it from SharedPreferences file to save up some space.
    // Since AN2Linux will default to false if the setting is not found.
    if (!newSettingsValue) {
        sp.edit().remove(key).apply();
    }
}
 
Example 3
Source File: EnabledApplicationsPreferenceFragment.java    From an2linuxclient with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
    boolean newSettingsValue = sp.getBoolean(key, false);
    String prefEnableDisableAllKey = getString(R.string.preference_enable_disable_all_applications);
    if (key.equals(prefEnableDisableAllKey)) {
        final PreferenceScreen prefScreen = findPreference(getString(R.string.enabled_apps_pref_screen));
        for (int i = 1; i < prefScreen.getPreferenceCount(); i++) {
            ((CheckBoxPreference) (prefScreen.getPreference(i))).setChecked(newSettingsValue);
        }
    }

    // If newSettingsValue == false, clear it from SharedPreferences file to save up some space.
    // Since AN2Linux will default to false if the setting is not found.
    if (!newSettingsValue) {
        sp.edit().remove(key).apply();
    }
}
 
Example 4
Source File: CommCarePreferenceFragment.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
private void setupLocalizedText() {
    Map<String, String> prefToTitleMap = getPrefKeyTitleMap();
    if (prefToTitleMap != null) {
        PreferenceScreen screen = getPreferenceScreen();
        for (int i = 0; i < screen.getPreferenceCount(); i++) {
            String key = screen.getPreference(i).getKey();
            if (prefToTitleMap.containsKey(key)) {
                try {
                    String localizedString = Localization.get(prefToTitleMap.get(key));
                    screen.getPreference(i).setTitle(localizedString);
                } catch (NoLocalizedTextException nle) {
                    Log.w(TAG, "Unable to localize: " + prefToTitleMap.get(key));
                }
            }
        }
    }
}
 
Example 5
Source File: DeveloperPreferences.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
private Preference[] getOnScreenPrefs() {
    PreferenceScreen prefScreen = getPreferenceScreen();
    Preference[] prefs = new Preference[prefScreen.getPreferenceCount()];
    for (int i = 0; i < prefScreen.getPreferenceCount(); i++) {
        prefs[i] = prefScreen.getPreference(i);
    }
    return prefs;
}