androidx.preference.PreferenceFragmentCompat Java Examples

The following examples show how to use androidx.preference.PreferenceFragmentCompat. 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: SettingsActivity.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
    // Instantiate the new Fragment
    final Bundle args = pref.getExtras();
    final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(
            getClassLoader(),
            pref.getFragment());
    fragment.setArguments(args);
    fragment.setTargetFragment(caller, 0);

    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frame_layout_settings_activity, fragment)
            .addToBackStack(null)
            .commit();
    setTitle(pref.getTitle());
    return true;
}
 
Example #2
Source File: PreferencesActivity.java    From SAI with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_preferences);

    mCoolbar = findViewById(R.id.coolbar);
    mCoolbar.setTitle(getIntent().getStringExtra(Intent.EXTRA_TITLE));

    FragmentManager fm = getSupportFragmentManager();
    PreferenceFragmentCompat mPreferencesFragment = (PreferenceFragmentCompat) fm.findFragmentByTag(TAG_PREFERENCES_FRAGMENT);
    if (mPreferencesFragment != null)
        return;

    mPreferencesFragment = createNewPrefsFragment();
    fm.beginTransaction().add(R.id.container, mPreferencesFragment, TAG_PREFERENCES_FRAGMENT).commit();
}
 
Example #3
Source File: PreferencesActivity.java    From Field-Book with GNU General Public License v2.0 6 votes vote down vote up
@Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, androidx.preference.Preference pref) {

    // Instantiate the new Fragment
    final Bundle args = pref.getExtras();
    final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(
            getClassLoader(),
            pref.getFragment());
    fragment.setArguments(args);
    fragment.setTargetFragment(caller, 0);

    // Replace the existing Fragment with the new Fragment
    getSupportFragmentManager().beginTransaction()
            .replace(android.R.id.content, fragment)
            .addToBackStack(null)
            .commit();
    return true;
}
 
Example #4
Source File: SettingsFragment.java    From shortyz with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller, PreferenceScreen preferenceScreen) {
    try {
        FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
        SettingsFragment fragment = new SettingsFragment();
        Bundle args = new Bundle();
        args.putString(PreferenceFragmentCompat.ARG_PREFERENCE_ROOT, preferenceScreen.getKey());
        fragment.setArguments(args);
        ft.add(R.id.settings_container, fragment, preferenceScreen.getKey());
        ft.addToBackStack(preferenceScreen.getKey());
        ft.commit();
    } catch(Exception e){
        e.printStackTrace();
        throw e;
    }
    return true;
}
 
Example #5
Source File: SearchPreferenceResult.java    From SearchPreference with MIT License 6 votes vote down vote up
/**
 * Alternative (old) highlight method if ripple does not work
 */
private void highlightFallback(PreferenceFragmentCompat prefsFragment, final Preference prefResult) {
    TypedValue typedValue = new TypedValue();
    Resources.Theme theme = prefsFragment.getActivity().getTheme();
    theme.resolveAttribute(android.R.attr.textColorPrimary, typedValue, true);
    TypedArray arr = prefsFragment.getActivity().obtainStyledAttributes(typedValue.data, new int[]{
            android.R.attr.textColorPrimary});
    int color = arr.getColor(0, 0xff3F51B5);
    arr.recycle();

    final Drawable oldIcon = prefResult.getIcon();
    final boolean oldSpaceReserved = prefResult.isIconSpaceReserved();
    Drawable arrow = AppCompatResources.getDrawable(prefsFragment.getContext(), R.drawable.searchpreference_ic_arrow_right);
    arrow.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    prefResult.setIcon(arrow);
    prefsFragment.scrollToPreference(prefResult);
    new Handler().postDelayed(() -> {
        prefResult.setIcon(oldIcon);
        prefResult.setIconSpaceReserved(oldSpaceReserved);
    }, 1000);
}
 
Example #6
Source File: SettingsActivity.java    From HgLauncher with GNU General Public License v3.0 6 votes vote down vote up
@Override public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
    // Instantiate the new Fragment
    final Fragment fragment = getSupportFragmentManager()
            .getFragmentFactory().instantiate(getClassLoader(), pref.getFragment());
    fragment.setTargetFragment(caller, 0);
    fragmentTitle = pref.getTitle();

    // Replace the existing Fragment with the new Fragment
    getSupportFragmentManager().beginTransaction()
                               .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                               .replace(R.id.fragment_container, fragment, pref.getKey())
                               .addToBackStack(pref.getKey())
                               .commit();

    // Update the Activity's action bar
    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle(fragmentTitle);
    }
    return true;
}
 
Example #7
Source File: SettingsActivity.java    From intra42 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
    // Instantiate the new Fragment
    final Bundle args = pref.getExtras();
    final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(
            getClassLoader(),
            pref.getFragment());
    fragment.setArguments(args);
    fragment.setTargetFragment(caller, 0);
    // Replace the existing Fragment with the new Fragment
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.settings, fragment)
            .addToBackStack(null)
            .commit();
    setTitle(pref.getTitle());
    return true;
}
 
Example #8
Source File: OnDisplayPreferenceDialogDelegater.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * PreferenceFragmentCompat#onDisplayPreferenceDialogのヘルパーメソッド
 * @param fragment
 * @param preference
 * @return true: 処理済み, false: 未処理
 */
public static boolean onDisplayPreferenceDialog(
	@NonNull final PreferenceFragmentCompat fragment,
	final Preference preference) {

	if (DEBUG) Log.v(TAG, "onDisplayPreferenceDialog:" + preference);
	final androidx.fragment.app.FragmentManager fm = fragment.getParentFragmentManager();
	if (preference instanceof NumberPickerPreferenceV7) {
		// check if dialog is already showing
		if (fm.findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {
			return true;	// handled
		}

		final androidx.fragment.app.DialogFragment f
			= NumberPickerPreferenceFragmentCompat.newInstance(preference.getKey());
		f.setTargetFragment(fragment, 0);
		f.show(fm, DIALOG_FRAGMENT_TAG);
		return true;	// handled
	}
	return false;
}
 
Example #9
Source File: SettingsActivity.java    From HgLauncher with GNU General Public License v3.0 6 votes vote down vote up
@Override public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
    // Instantiate the new Fragment
    final Fragment fragment = getSupportFragmentManager()
            .getFragmentFactory().instantiate(getClassLoader(), pref.getFragment());
    fragment.setTargetFragment(caller, 0);
    fragmentTitle = pref.getTitle();

    // Replace the existing Fragment with the new Fragment
    getSupportFragmentManager().beginTransaction()
                               .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                               .replace(R.id.fragment_container, fragment, pref.getKey())
                               .addToBackStack(pref.getKey())
                               .commit();

    // Update the Activity's action bar
    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle(fragmentTitle);
    }
    return true;
}
 
Example #10
Source File: PreferencesActivity.java    From TowerCollector with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
    // instantiate the new Fragment
    final Bundle args = pref.getExtras();
    final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(
            getClassLoader(),
            pref.getFragment());
    fragment.setArguments(args);
    fragment.setTargetFragment(caller, 0);
    // replace the existing Fragment with the new Fragment
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.preferences_container, fragment)
            .addToBackStack(null)
            .commit();
    setTitle(pref.getTitle());
    return true;
}
 
Example #11
Source File: SettingsActivity.java    From AndroidPreferenceActivity with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the navigation preference, which allows to navigate to the appearance settings.
 *
 * @param fragment
 *         The fragment, which contains the preference, as an instance of the class {@link
 *         PreferenceFragmentCompat}. The fragment may not be null
 */
private void initializeAppearanceNavigationPreference(
        @NonNull final PreferenceFragmentCompat fragment) {
    String key = getString(R.string.appearance_navigation_preference_key);
    NavigationPreference navigationPreference =
            (NavigationPreference) fragment.findPreference(key);
    Bundle extras = new Bundle();
    extras.putBoolean(PreferenceFragment.EXTRA_SHOW_RESTORE_DEFAULTS_BUTTON, true);
    navigationPreference.setExtras(extras);

}
 
Example #12
Source File: PreferenceSettingsUtils.java    From talkback with Apache License 2.0 5 votes vote down vote up
/** Finds the preference by the key ID. */
public static @Nullable Preference findPreference(AppCompatActivity activity, int prefKeyId) {
  PreferenceFragmentCompat fragment =
      (PreferenceFragmentCompat)
          activity.getSupportFragmentManager().findFragmentById(android.R.id.content);
  if (fragment == null) {
    return null;
  }
  return fragment.findPreference(activity.getString(prefKeyId));
}
 
Example #13
Source File: PreferenceSettingsUtils.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Inflates the given XML resource and adds the preference hierarchy to the current preference
 * hierarchy. {@link PreferenceFragmentCompat#addPreferencesFromResource}
 */
public static void addPreferencesFromResource(
    PreferenceFragmentCompat preferenceFragment, @XmlRes int preferencesResId) {
  // Set preferences to use device-protected storage.
  if (BuildVersionUtils.isAtLeastN()) {
    preferenceFragment.getPreferenceManager().setStorageDeviceProtected();
  }
  preferenceFragment.addPreferencesFromResource(preferencesResId);
}
 
Example #14
Source File: PreferenceSettingsUtils.java    From talkback with Apache License 2.0 5 votes vote down vote up
/**
 * Inflates the given XML resource and replaces the current preference hierarchy (if any) with the
 * preference hierarchy rooted at {@code key}.{@link
 * PreferenceFragmentCompat#setPreferencesFromResource(int, String)}
 */
public static void setPreferencesFromResource(
    PreferenceFragmentCompat preferenceFragment, @XmlRes int preferencesResId, String key) {
  // Set preferences to use device-protected storage.
  if (BuildVersionUtils.isAtLeastN()) {
    preferenceFragment.getPreferenceManager().setStorageDeviceProtected();
  }
  preferenceFragment.setPreferencesFromResource(preferencesResId, key);
}
 
Example #15
Source File: SettingsActivity.java    From AndroidPreferenceActivity with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes the navigation preference, which allows to navigate to the behavior settings.
 *
 * @param fragment
 *         The fragment, which contains the preference, as an instance of the class {@link
 *         PreferenceFragmentCompat}. The fragment may not be null
 */
private void initializeBehaviorNavigationPreference(
        @NonNull final PreferenceFragmentCompat fragment) {
    String key = getString(R.string.behavior_navigation_preference_key);
    NavigationPreference navigationPreference =
            (NavigationPreference) fragment.findPreference(key);
    Bundle extras = new Bundle();
    extras.putBoolean(PreferenceFragment.EXTRA_SHOW_RESTORE_DEFAULTS_BUTTON, true);
    navigationPreference.setExtras(extras);
}
 
Example #16
Source File: SettingsActivity.java    From mhzs with MIT License 5 votes vote down vote up
/**
     * This method stops fragment injection in malicious applications.
     * Make sure to deny any unknown fragments here.
     */
    protected boolean isValidFragment(String fragmentName) {
        return PreferenceFragmentCompat.class.getName()
                       .equals(fragmentName) || SettingFragment.class.getName()
//                       .equals(fragmentName) || ConversationPreferenceFragment.class.getName()
//                       .equals(fragmentName) || ContactsPreferenceFragment.class.getName()
//                       .equals(fragmentName) || LebaPreferenceFragment.class.getName()
//                       .equals(fragmentName) || ChatPreferenceFragment.class.getName()
//                       .equals(fragmentName) || TroopPreferenceFragment.class.getName()
//                       .equals(fragmentName) || ExtensionPreferenceFragment.class.getName()
//                       .equals(fragmentName) || SettingPreferenceFragment.class.getName()
//                       .equals(fragmentName) || AboutPreferenceFragment.class.getName()
                       .equals(fragmentName);
    }
 
Example #17
Source File: SettingsActivity.java    From GeometricWeather with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void pushFragment(PreferenceFragmentCompat f, String tag) {
    getSupportFragmentManager()
            .beginTransaction()
            .setCustomAnimations(android.R.anim.fade_in, 0, android.R.anim.fade_in, 0)
            .replace(R.id.activity_settings_container, f)
            .addToBackStack(tag)
            .commit();
}
 
Example #18
Source File: PreferencesActivity.java    From SAI with GNU General Public License v3.0 5 votes vote down vote up
private PreferenceFragmentCompat createNewPrefsFragment() {
    try {
        Class<? extends PreferenceFragmentCompat> prefsFragmentClass = (Class<? extends PreferenceFragmentCompat>) Class.forName(getIntent().getStringExtra(EXTRA_PREF_FRAGMENT_CLASS));
        Constructor zeroArgsConstructor = prefsFragmentClass.getConstructor();
        return (PreferenceFragmentCompat) zeroArgsConstructor.newInstance();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example #19
Source File: InfoButtonListPreferencePref.java    From vinyl-cast with MIT License 5 votes vote down vote up
/**
 * Helper method to trigger a refresh of the provided PrefernceFragment's listview
 * holding audioEncodingPref to make sure the info button gets redrawn with latest state
 * @param parentPrefFragment the PreferenceFragment that is holding this instance
 *                           of InfoButtonListPreference
 */
public void notifyPreferenceListItemChanged(PreferenceFragmentCompat parentPrefFragment) {
    RecyclerView prefListView = parentPrefFragment.getListView();
    if (prefListView != null) {
        int position = prefListView.getChildAdapterPosition(getView());
        if (position != RecyclerView.NO_POSITION) {
            prefListView.getAdapter().notifyItemChanged(position);
        }
    }
}
 
Example #20
Source File: SettingsActivity.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
    // Instantiate the new Fragment
    final Bundle args = pref.getExtras();
    final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(getClassLoader(), pref.getFragment());
    fragment.setArguments(args);
    fragment.setTargetFragment(caller, 0);
    // Replace the existing Fragment with the new Fragment
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.settings, fragment)
            .addToBackStack(null)
            .commit();
    setTitle(pref.getTitle());
    return true;
}
 
Example #21
Source File: SettingsActivity.java    From Android-Developer-Fundamentals-Version-2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) {
    // Instantiate the new Fragment
    final Bundle args = pref.getExtras();
    final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate(getClassLoader(), pref.getFragment());
    fragment.setArguments(args);
    fragment.setTargetFragment(caller, 0);
    // Replace the existing Fragment with the new Fragment
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.settings, fragment)
            .addToBackStack(null)
            .commit();
    setTitle(pref.getTitle());
    return true;
}
 
Example #22
Source File: SearchPreferenceResult.java    From SearchPreference with MIT License 5 votes vote down vote up
private void doHighlight(final PreferenceFragmentCompat prefsFragment) {
    final Preference prefResult = prefsFragment.findPreference(getKey());

    if (prefResult == null) {
        Log.e("doHighlight", "Preference not found on given screen");
        return;
    }
    final RecyclerView recyclerView = prefsFragment.getListView();
    final RecyclerView.Adapter<?> adapter = recyclerView.getAdapter();
    if (adapter instanceof PreferenceGroup.PreferencePositionCallback) {
        PreferenceGroup.PreferencePositionCallback callback = (PreferenceGroup.PreferencePositionCallback) adapter;
        final int position = callback.getPreferenceAdapterPosition(prefResult);
        if (position != RecyclerView.NO_POSITION) {
            recyclerView.scrollToPosition(position);
            recyclerView.postDelayed(() -> {
                RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(position);
                if (holder != null) {
                    Drawable background = holder.itemView.getBackground();
                    if (Build.VERSION.SDK_INT >= 21 && background instanceof RippleDrawable) {
                        forceRippleAnimation((RippleDrawable) background);
                        return;
                    }
                }
                highlightFallback(prefsFragment, prefResult);
            }, 200);
            return;
        }
    }
    highlightFallback(prefsFragment, prefResult);
}
 
Example #23
Source File: SettingsActivity.java    From APDE with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean onPreferenceStartScreen(androidx.preference.PreferenceFragmentCompat preferenceFragmentCompat, PreferenceScreen preferenceScreen) {
	return false;
}
 
Example #24
Source File: PreferencesActivity.java    From geopackage-mapcache-android with MIT License 4 votes vote down vote up
@Override
public boolean onPreferenceStartFragment(PreferenceFragmentCompat preferenceFragmentCompat, Preference preference) {
    return false;
}
 
Example #25
Source File: TalkBackShortcutPreferencesActivity.java    From talkback with Apache License 2.0 4 votes vote down vote up
@Override
protected PreferenceFragmentCompat createPreferenceFragment() {
  return new ShortcutPrefFragment();
}
 
Example #26
Source File: TalkBackDeveloperPreferencesActivity.java    From talkback with Apache License 2.0 4 votes vote down vote up
@Override
protected PreferenceFragmentCompat createPreferenceFragment() {
  return new DeveloperPrefFragment();
}
 
Example #27
Source File: TalkBackVerbosityPreferencesActivity.java    From talkback with Apache License 2.0 4 votes vote down vote up
@Override
protected PreferenceFragmentCompat createPreferenceFragment() {
  return new VerbosityPrefFragment(getApplicationContext());
}
 
Example #28
Source File: TalkBackPreferencesActivity.java    From talkback with Apache License 2.0 4 votes vote down vote up
@Override
protected PreferenceFragmentCompat createPreferenceFragment() {
  return new TalkBackPreferenceFragment();
}
 
Example #29
Source File: TalkBackDumpAccessibilityEventActivity.java    From talkback with Apache License 2.0 4 votes vote down vote up
@Override
protected PreferenceFragmentCompat createPreferenceFragment() {
  TalkBackDumpAccessibilityEventFragment fragment = new TalkBackDumpAccessibilityEventFragment();
  listener = fragment;
  return fragment;
}
 
Example #30
Source File: TalkBackSelectorPreferencesActivity.java    From talkback with Apache License 2.0 4 votes vote down vote up
@Override
protected PreferenceFragmentCompat createPreferenceFragment() {
  return new SelectorPrefFragment();
}