Making a button transparent then opaque again in android

279 views Asked by At

I have a game where I am using 12 buttons with no text on to display lives. Every time they lose a life, this code is run

public void guessesRemainingDisplay(int numberOfGuesses) {

    int guessesRemaining;

    guessesRemaining = maximumGuesses + 1 - numberOfGuesses;

    switch(guessesRemaining) {

    case 1:

        findViewById(R.id.Guess1).setBackgroundColor(color.transparent);
        break;

    case 2:
        findViewById(R.id.Guess2).setBackgroundColor(color.transparent);
        break;

    case 3:
        findViewById(R.id.Guess3).setBackgroundColor(color.transparent);
        break;

    case 4:
        findViewById(R.id.Guess4).setBackgroundColor(color.transparent);
        break;

    case 5:
        findViewById(R.id.Guess5).setBackgroundColor(color.transparent);
        break;

    case 6:
        findViewById(R.id.Guess6).setBackgroundColor(color.transparent);
        break;

    case 7:
        findViewById(R.id.Guess7).setBackgroundColor(color.transparent);
        break;

    case 8:
        findViewById(R.id.Guess8).setBackgroundColor(color.transparent);
        break;

    case 9:
        findViewById(R.id.Guess9).setBackgroundColor(color.transparent);
        break;

    case 10:
        findViewById(R.id.Guess10).setBackgroundColor(color.transparent);
        break;

    case 11:
        findViewById(R.id.Guess11).setBackgroundColor(color.transparent);
        break;

    case 12:
        findViewById(R.id.Guess12).setBackgroundColor(color.transparent);
        break;


    }

}

And the furthest most right button disappears (they are in a line, 1 on left 12 on right).

However, when I start a new game, or when the activity first opens, this code is run

findViewById(R.id.Guess1).setBackgroundColor(color.X);

Repeated for every single id. X being literally any color (I've tried it with loads of different ones). For some reason, if this code is ever run, the button disappears. Why? If its not run, the 12 buttons appear, but whenever I start a new game obviously the buttons that have disappeared due to losing lives dont come back.

1

There are 1 answers

0
Jin35 On BEST ANSWER

For you task it's better to use findViewById(R.id.id).setVisibility(View.Invisible) for disappearing and 'findViewById(R.id.id).setVisibility(View.Visible)' for returning view back.