Skip to content Skip to sidebar Skip to footer

Why Does Setalpha() Act On All My Buttons Whilst Setimageresource() Acts On Just One Button?

At the moment my buttons do not work. The first two times any are pressed all buttons are influenced rather than just the one that has been pressed. Swap: seatButton[i].setAlpha(2

Solution 1:

You're doing a String comparison with ==, which means you're comparing references and not values. This is probably not what you want, so you should change that from:

if(table.seats[i].getState() == "empty") { ... }

to:

if(table.seats[i].getState().equals("empty")) { ... }

Besides that, according to the documentation of setAlpha(float alpha) (which is an API 11 method, just for reference), the passed float should be between [0...1].

The image resource you're setting is the ImageManager's R.id.transparent_background. This may suggest that your logic works, but the error is indeed somewhere in setting the alpha value.

Solution 2:

I've found a solution yet I do not understand it nor am I happy with it. I set up an alpha animation that changed the alpha value of all my buttons from 255 to 255 upon opening my program. In other words it does nothing. However my program now works. I would welcome a better solution or an explanation as to why this works?

This code is placed with the rest of initializations at the start of the onCreate() method. It sets up an AlphaAnimation that remain at the same value.

//Initializes AlphaAnimations to alter transparency
alphaDown = new AlphaAnimation(1f, 1f); 
alphaDown.setDuration(1000);
alphaDown.setFillAfter(true);

This is the same loop shown at the bottom of my question with one line changed. It activates this stationary animation before setting all my buttons as translucent. Without this animation all buttons are affected with one click. With the animation each button responds when it and only it has been clicked.

//Assigns the buttons and stats panels defined in the layout xml to their appropiate java arrays. Also sets clickListeners to buttons.for(int i = 0; i < 10; i++)
    {
        seatButton[i] = (ImageButton) findViewById(getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud"));
        seatStats[i] = (TextView) findViewById(getResources().getIdentifier("textView" + (i+1), "id", "en.deco.android.livehud"));
        seatButton[i].setOnLongClickListener(longClickListener);
        seatButton[i].startAnimation(alphaDown);
        seatButton[i].setAlpha(80);
    }

Post a Comment for "Why Does Setalpha() Act On All My Buttons Whilst Setimageresource() Acts On Just One Button?"