Why wont the button disappear?

Im trying to develop an Android app, and one of the features when hitting a button, should be that the button disappears. This, however does not work, can anyone explain me why? Thanks!

    Button StartButton = (Button) findViewById(R.id.startButton);
    StartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
                View thisButton = findViewById(R.id.startButton);
                thisButton.setVisibility(View.GONE);
        }

    });

Upvotes: 0

Views: 74

Answers (7)

DKV
DKV

Reputation: 1767

please use the button. You only need to hide the particular button

  StartButton.setVisibility(View.GONE);

Upvotes: 0

Im sorry for answering my own question, but there was a problem with the studio, and not the app. Sorry for making such a big deal out of nothing. Thanks for all answers. They helped me alot when setting up a new project :)

Upvotes: 0

Sergey
Sergey

Reputation: 65

Try it like this:

    Button StartButton = (Button) findViewById(R.id.startButton);        
    StartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            view.setVisibility(View.GONE);
        }
    });

Upvotes: 0

Lucas Ferraz
Lucas Ferraz

Reputation: 4152

Try the following:

    Button StartButton = (Button) findViewById(R.id.startButton);        
        StartButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                view.setVisibility(View.GONE);
            }
        });

Upvotes: 1

KDeogharkar
KDeogharkar

Reputation: 10959

 Button StartButton = (Button) findViewById(R.id.startButton);
    StartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
                view.setVisibility(View.GONE);
        }

    });

or make button final and onclick change visibility.

final Button StartButton = (Button) findViewById(R.id.startButton);
 StartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view){
                    StartButton.setVisibility(View.GONE);
            }

        });

Upvotes: 0

Jas
Jas

Reputation: 3212

Try :

StartButton.setVisibility(View.GONE);

Upvotes: 0

Lubomir Babev
Lubomir Babev

Reputation: 1908

You don't need to initialize the button again. Just pass the same button.Try with this :

Button StartButton = (Button) findViewById(R.id.startButton);
    StartButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
                StartButton.setVisibility(View.GONE);
        }

    });

Upvotes: 0

Related Questions