Is possible to hide a preference in a PreferenceScreen? I don't need to disable it, it must be invisible (sometimes)
Important: I need to keep the min API level 7+
Is possible to hide a preference in a PreferenceScreen? I don't need to disable it, it must be invisible (sometimes)
Important: I need to keep the min API level 7+
If your logout button (Preference) is in the PreferenceScreen, do this:
PreferenceScreen screen = getPreferenceScreen();
Preference logout = findPreference("logout");
if(screen != null && logout != null)
screen.removePreference(logout);
Else if your logout button (Preference) is in a PreferenceCategory (which is inside a PreferenceScreen), do this:
PreferenceCategory category = (PreferenceCategory) findPreference("category_name");
Preference logout = findPrefence("logout");
if(category != null && logout != null)
category.removePreference(logout);
You can put whatever your preferences name is, this is for example for a logout preference, if you have another Preference (eg CheckBoxPreference) you need to cast that specific Preference before findPreference.
Something like that should works:
Preference p = findPreference("your_preference_key");
getPreferenceScreen().removePreference(p);
PreferenceScreen scr = getPreferenceScreen();
if(scr!=null)
scr.removePreference(findPreference("preferenceKey"));
If you use Support Library v7 Preference, you can use the setVisible
method. It does exactly what you need.
You can now do this directly in xml with the AppCompat library.
See https://mcmap.net/q/157624/-remove-hide-a-preference-from-the-screen
© 2022 - 2024 — McMap. All rights reserved.