MataMix
MataMix

Reputation: 3296

Hide preference button on one preferences page

I built a preferences xml that I use as resource in two different activity pages.

The only problem is that I don't want the LOGOUT button to show in one of those two pages (because the user is not connected yet).

What I did now is:

logoutButton = (Preference)getPreferenceScreen().findPreference("logout");
logoutButton.setEnabled(false);

the button now shows up in gray.. but is there a way to not make it show at all??

Thanks!

Upvotes: 1

Views: 1540

Answers (3)

vallllll
vallllll

Reputation: 2761

put some condition and add this to the oncreate method for example

if(something){
    CheckBoxPreference lp=new CheckBoxPreference(this);
    lp.setKey("checkbox");
    lp.setTitle("logout");
    lp.setEnabled(true);
    getPreferenceScreen().addPreference(lp);
    }

if I put something at true the preference shows otherwise it doesn't. Don't put it in the xml just in code

Upvotes: 0

Carnal
Carnal

Reputation: 22064

If your logout button (Preference) is in the PreferenceScreen, do this:

PreferenceScreen screen = getPreferenceScreen();
Preference logout = findPreference("logout");
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");
category.removePreference(logout);

Upvotes: 10

mik3y
mik3y

Reputation: 4284

Unlike Views, Preferences don't have a setVisibility method. Instead, try using PreferenceScreen#removePreference:

PreferenceScreen preferenceScreen = getPreferenceScreen();
logoutButton = (Preference) preferenceScreen.findPreference("logout");
preferenceScreen.removePreference(logoutButton);

You may need to retain a reference to logoutButton in your activity if you later wish to add it back.

Upvotes: 2

Related Questions