ABS Softwares ABS Softwares - 1 year ago 54
C# Question

How to hide a button after clicked for ever in android?

I want to hide a button when it's clicked for ever.

I tried this code:


But when I close and open the app, the button will re-show again .

So how can I hide the button for ever? Thank you :)

Answer Source

You can't technically hide it "forever" but you can do this:

First, declare this variable in your Activity/Fragment:

public static final String SHARED_KEY_BUTTON_HIDE = "my_button_hidden";

Let's say this is your button:

final Button myButton = (Button) findViewById(R.id.button);

Here's the setOnClickListenr for the Button:

myButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(SHARED_KEY_BUTTON_HIDE, true);

Here's the key part:

When that Activity start, do this in onCreate() after you declare the button:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean hideButton = preferences.getBoolean(SHARED_KEY_BUTTON_HIDE, false);

if(hideButton) {

This will do the job.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download