Hide a preference in a PreferenceActivity
Asked Answered
C

5

6

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+

Corrugate answered 3/11, 2011 at 8:19 Comment(0)
H
19

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.

Howell answered 3/11, 2011 at 8:42 Comment(0)
T
4

Something like that should works:

Preference p = findPreference("your_preference_key");
getPreferenceScreen().removePreference(p);
Thurgau answered 3/11, 2011 at 8:27 Comment(0)
L
1

PreferenceScreen scr = getPreferenceScreen();

if(scr!=null)

     scr.removePreference(findPreference("preferenceKey"));
Linchpin answered 24/12, 2013 at 10:29 Comment(0)
N
0

If you use Support Library v7 Preference, you can use the setVisible method. It does exactly what you need.

Nevers answered 12/7, 2018 at 9:9 Comment(0)
H
0

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

Halfhearted answered 11/1, 2019 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.