Java Code Examples for androidx.preference.SwitchPreference#setVisible()

The following examples show how to use androidx.preference.SwitchPreference#setVisible() . 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: GesturesAndButtonsPreferenceFragment.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.gestures_and_buttons_preference, rootKey);
    ((Infinity) activity.getApplication()).getAppComponent().inject(this);

    SwitchPreference lockJumpToNextTopLevelCommentButtonSwitch =
            findPreference(SharedPreferencesUtils.LOCK_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON);
    SwitchPreference lockBottomAppBarSwitch = findPreference(SharedPreferencesUtils.LOCK_BOTTOM_APP_BAR);
    SwitchPreference swipeUpToHideJumpToNextTopLevelCommentButtonSwitch =
            findPreference(SharedPreferencesUtils.SWIPE_UP_TO_HIDE_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON);

    if (lockJumpToNextTopLevelCommentButtonSwitch != null && lockBottomAppBarSwitch != null &&
            swipeUpToHideJumpToNextTopLevelCommentButtonSwitch != null) {
        lockJumpToNextTopLevelCommentButtonSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
            if ((Boolean) newValue) {
                swipeUpToHideJumpToNextTopLevelCommentButtonSwitch.setVisible(false);
            } else {
                swipeUpToHideJumpToNextTopLevelCommentButtonSwitch.setVisible(true);
            }
            return true;
        });

        if (sharedPreferences.getBoolean(SharedPreferencesUtils.BOTTOM_APP_BAR_KEY, false)) {
            lockBottomAppBarSwitch.setVisible(true);
            lockBottomAppBarSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
                EventBus.getDefault().post(new ChangeLockBottomAppBarEvent((Boolean) newValue));
                return true;
            });
        }

        if (!sharedPreferences.getBoolean(SharedPreferencesUtils.LOCK_JUMP_TO_NEXT_TOP_LEVEL_COMMENT_BUTTON, false)) {
            swipeUpToHideJumpToNextTopLevelCommentButtonSwitch.setVisible(true);
        }
    }
}
 
Example 2
Source File: ImmersiveInterfacePreferenceFragment.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.immersive_interface_preferences, rootKey);

    SwitchPreference immersiveInterfaceSwitch = findPreference(SharedPreferencesUtils.IMMERSIVE_INTERFACE_KEY);
    SwitchPreference immersiveInterfaceIgnoreNavBarSwitch = findPreference(SharedPreferencesUtils.IMMERSIVE_INTERFACE_IGNORE_NAV_BAR_KEY);

    if (immersiveInterfaceSwitch != null && immersiveInterfaceIgnoreNavBarSwitch != null) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            immersiveInterfaceSwitch.setVisible(true);
            immersiveInterfaceSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
                if ((Boolean) newValue) {
                    immersiveInterfaceIgnoreNavBarSwitch.setVisible(true);
                } else {
                    immersiveInterfaceIgnoreNavBarSwitch.setVisible(false);
                }
                EventBus.getDefault().post(new RecreateActivityEvent());
                return true;
            });

            if (immersiveInterfaceSwitch.isChecked()) {
                immersiveInterfaceIgnoreNavBarSwitch.setVisible(true);
            } else {
                immersiveInterfaceIgnoreNavBarSwitch.setVisible(false);
            }

            immersiveInterfaceIgnoreNavBarSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
                EventBus.getDefault().post(new RecreateActivityEvent());
                return true;
            });
        } else {
            immersiveInterfaceSwitch.setVisible(false);
            immersiveInterfaceIgnoreNavBarSwitch.setVisible(false);
        }
    }
}
 
Example 3
Source File: MainPreferenceFragment.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.main_preferences, rootKey);
    ((Infinity) activity.getApplication()).getAppComponent().inject(this);

    SwitchPreference confirmToExitSwitch = findPreference(SharedPreferencesUtils.CONFIRM_TO_EXIT);
    SwitchPreference nsfwSwitch = findPreference(SharedPreferencesUtils.NSFW_KEY);
    SwitchPreference blurNSFWSwitch = findPreference(SharedPreferencesUtils.BLUR_NSFW_KEY);
    SwitchPreference blurSpoilerSwitch = findPreference(SharedPreferencesUtils.BLUR_SPOILER_KEY);

    if (confirmToExitSwitch != null) {
        confirmToExitSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
            EventBus.getDefault().post(new RecreateActivityEvent());
            return true;
        });
    }

    if (nsfwSwitch != null) {
        nsfwSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
            EventBus.getDefault().post(new ChangeNSFWEvent((Boolean) newValue));
            if (blurNSFWSwitch != null) {
                blurNSFWSwitch.setVisible((Boolean) newValue);
            }
            return true;
        });
    }

    if (blurNSFWSwitch != null) {
        boolean nsfwEnabled = sharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_KEY, false);

        if (nsfwEnabled) {
            blurNSFWSwitch.setVisible(true);
        } else {
            blurNSFWSwitch.setVisible(false);
        }

        blurNSFWSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
            EventBus.getDefault().post(new ChangeNSFWBlurEvent((Boolean) newValue));
            return true;
        });
    }

    if (blurSpoilerSwitch != null) {
        blurSpoilerSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
            EventBus.getDefault().post(new ChangeSpoilerBlurEvent((Boolean) newValue));
            return true;
        });
    }
}
 
Example 4
Source File: PreferencesFragment.java    From SAI with GNU General Public License v3.0 4 votes vote down vote up
@SuppressLint("ApplySharedPref")
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.preferences_main, rootKey);

    mHelper = PreferencesHelper.getInstance(requireContext());

    mHomeDirPref = findPreference("home_directory");
    updateHomeDirPrefSummary();
    mHomeDirPref.setOnPreferenceClickListener((p) -> {
        selectHomeDir();
        return true;
    });

    mFilePickerSortPref = findPreference("file_picker_sort");
    updateFilePickerSortSummary();
    mFilePickerSortPref.setOnPreferenceClickListener((p) -> {
        SingleChoiceListDialogFragment.newInstance(getText(R.string.settings_main_file_picker_sort), R.array.file_picker_sort_variants, mHelper.getFilePickerRawSort()).show(getChildFragmentManager(), "sort");
        return true;
    });

    findPreference("about").setOnPreferenceClickListener((p) -> {
        startActivity(new Intent(getContext(), AboutActivity.class));
        return true;
    });
    findPreference("donate").setOnPreferenceClickListener(p -> {
        startActivity(new Intent(requireContext(), DonateActivity.class));
        return true;
    });

    mInstallerPref = findPreference("installer");
    updateInstallerSummary();
    mInstallerPref.setOnPreferenceClickListener((p -> {
        SingleChoiceListDialogFragment.newInstance(getText(R.string.settings_main_installer), R.array.installers, mHelper.getInstaller()).show(getChildFragmentManager(), "installer");
        return true;
    }));

    findPreference(PreferencesKeys.BACKUP_SETTINGS).setOnPreferenceClickListener(p -> {
        startActivity(new Intent(requireContext(), BackupSettingsActivity.class));
        return true;
    });

    mThemePref = findPreference(PreferencesKeys.THEME);
    updateThemeSummary();
    mThemePref.setOnPreferenceClickListener(p -> {
        ThemeSelectionDialogFragment.newInstance(requireContext()).show(getChildFragmentManager(), "theme");
        return true;
    });
    if (Theme.getInstance(requireContext()).getThemeMode() != Theme.Mode.CONCRETE) {
        mThemePref.setVisible(false);
    }

    mAutoThemeSwitch = Objects.requireNonNull(findPreference(PreferencesKeys.AUTO_THEME));
    mAutoThemePicker = findPreference(PreferencesKeys.AUTO_THEME_PICKER);
    updateAutoThemePickerSummary();

    mAutoThemeSwitch.setOnPreferenceChangeListener((preference, newValue) -> {
        boolean value = (boolean) newValue;
        if (value) {
            if (!Utils.apiIsAtLeast(Build.VERSION_CODES.Q))
                SimpleAlertDialogFragment.newInstance(requireContext(), R.string.settings_main_auto_theme, R.string.settings_main_auto_theme_pre_q_warning).show(getChildFragmentManager(), null);

            Theme.getInstance(requireContext()).setMode(Theme.Mode.AUTO_LIGHT_DARK);
        } else {
            Theme.getInstance(requireContext()).setMode(Theme.Mode.CONCRETE);
        }

        //Hack to not mess with hiding/showing preferences manually
        requireActivity().recreate();
        return true;
    });

    mAutoThemePicker.setOnPreferenceClickListener(pref -> {
        DarkLightThemeSelectionDialogFragment.newInstance().show(getChildFragmentManager(), null);
        return true;
    });

    if (Theme.getInstance(requireContext()).getThemeMode() != Theme.Mode.AUTO_LIGHT_DARK) {
        mAutoThemePicker.setVisible(false);
    }

    SwitchPreference firebasePref = findPreference(PreferencesKeys.ENABLE_FIREBASE);
    firebasePref.setOnPreferenceChangeListener((preference, newValue) -> {
        Firebase.setDataCollectionEnabled(requireContext(), (boolean) newValue);
        return true;
    });
    if (BuildConfig.IS_FLOSS_BUILD)
        firebasePref.setVisible(false);


    getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
 
Example 5
Source File: SettingsFragment.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
private void initDisplayPart() {
    // saturation animation enabled.
    SwitchPreference saturationEnabled = findPreference(getString(R.string.key_saturation_animation_enabled));
    saturationEnabled.setOnPreferenceChangeListener((preference, newValue) -> {
        SettingsServiceIMP.getInstance(requireActivity())
                .setSaturationAnimationEnabled((Boolean) newValue);
        return true;
    });

    // saturation animation duration.
    ListPreference duration = findPreference(getString(R.string.key_saturation_animation_duration));
    duration.setSummary(
            getNameByValue(
                    ComponentFactory.getSettingsService().getSaturationAnimationDuration(),
                    R.array.saturation_animation_durations,
                    R.array.saturation_animation_duration_values
            )
    );
    duration.setOnPreferenceChangeListener((preference, newValue) -> {
        SettingsServiceIMP.getInstance(requireActivity())
                .setSaturationAnimationDuration((String) newValue);
        preference.setSummary(
                getNameByValue(
                        (String) newValue,
                        R.array.saturation_animation_durations,
                        R.array.saturation_animation_duration_values
                )
        );
        return true;
    });

    // grid list in port.
    SwitchPreference gridPort = findPreference(getString(R.string.key_grid_list_in_port));
    gridPort.setVisible(DisplayUtils.isTabletDevice(requireActivity()));
    gridPort.setOnPreferenceChangeListener((preference, newValue) -> {
        showRebootSnackbar();
        return true;
    });

    // grid list in land.
    findPreference(getString(R.string.key_grid_list_in_land)).setOnPreferenceChangeListener((p, v) -> {
        showRebootSnackbar();
        return true;
    });
}
 
Example 6
Source File: SettingsFragment.java    From c3nav-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.preferences, rootKey);

    useWifiLocating = (CheckBoxPreference)this.findPreference(getString(R.string.use_wifi_locating_key));
    sharePrefs = PreferenceManager.getDefaultSharedPreferences(getContext());
    if (sharePrefs.getBoolean(getString(R.string.use_wifi_locating_key), true) && !checkLocationPermisson()) {
        useWifiLocating.setChecked(false);
    }
    useWifiLocating.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (!(boolean)newValue) {
                // always let user disable wifi locating
                return true;
            }

            boolean permissionAsked = false;
            while (permissionAsked == false) {
                if (checkLocationPermisson()) {
                    return true;
                }
                permissionAsked = true;
                ActivityCompat.requestPermissions(getActivity(),
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MainActivity.PERM_REQUEST);
            }
            return false;
        }
    });

    developerSettings = (PreferenceCategory)findPreference(getString(R.string.developer_settings_key));
    developerModeEnabled = (SwitchPreference)findPreference(getString(R.string.developer_mode_enabled_key));
    developerInstanceUrl = (EditTextPreference)findPreference(getString(R.string.developer_instance_url_key));
    developerHttpUser = (EditTextPreference)findPreference(getString(R.string.developer_http_user_key));
    developerHttpPassword = (EditTextPreference)findPreference(getString(R.string.developer_http_password_key));

    developerSettings.setVisible(BuildConfig.DEBUG);
    developerModeEnabled.setVisible(BuildConfig.DEBUG);
    developerInstanceUrl.setVisible(BuildConfig.DEBUG);
    developerHttpUser.setVisible(BuildConfig.DEBUG);
    developerHttpPassword.setVisible(BuildConfig.DEBUG);
}