Maryna Bern
Maryna Bern

Reputation: 3

setVisibility to View using Integer

I'm trying to set visibility in Android to many views at once and I want to send Integer to this views instead copying and pasting my code.

        if (gameIsActive == false) {

        startButton.setVisibility(startButton.VISIBLE);
        greetingTextView.setVisibility(greetingTextView.VISIBLE);

        gridLayout.setVisibility(View.INVISIBLE);
        timerTextView.setVisibility(View.INVISIBLE);
        scoreTextView.setVisibility(View.INVISIBLE);
        checkerTextView.setVisibility(View.INVISIBLE);

    } else {

        startButton.setVisibility(startButton.INVISIBLE);
        greetingTextView.setVisibility(greetingTextView.INVISIBLE);

        gridLayout.setVisibility(View.VISIBLE);
        timerTextView.setVisibility(View.VISIBLE);
        scoreTextView.setVisibility(View.VISIBLE);
        checkerTextView.setVisibility(View.VISIBLE);

}

I know that INVISIBLE = 4 and VISIBLE = 0, but creating int doesn't help.

int isVisible = 0;
startButton.setVisibility(startButton.isVisible);

How can I switch visible to invisible?

Upvotes: 0

Views: 2189

Answers (4)

Zubair Soomro
Zubair Soomro

Reputation: 180

You can also do something like this

Button.setVisibility(isVisible == 0 ? View.Visible : View.Invisible);

Where View.Visible = 0, View.Invisible = 4 & View.Gone = 8.

you can just pass the integer value too. if you are getting some kind of error share the log then.

Upvotes: 0

Suraj
Suraj

Reputation: 165

Try something like this

public void changeVisibility(int visibility)
{
    startButton.setVisibility(visibility);
    // you can add here as many as views you want
}

and

changeVisibility(View.VISIBLE);

Upvotes: 1

H.Taras
H.Taras

Reputation: 832

I had something like that:

void setViewsVisibility(int visibility){
    view.setVisibility(visibility);
    //oher views
}

And use it like that:

setViewsVisibility(View.GONE);

Hope it will help!

Upvotes: 2

Caspar Geerlings
Caspar Geerlings

Reputation: 1061

Have you tried:

startButton.setVisibility(isVisible);

Upvotes: 0

Related Questions