0

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(255);

For:

seatButton[i].setImageResource(0x7f020007)

And my code works! Only the button I press is effected. Why?

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    table = new Table(); //Creates Table
    seatButton = new ImageButton[10]; //Creates Array of buttons
    seatStats = new TextView[10]; //Creates array for stat panels


    //Creates longClickListener, used for players to sit in or out.
    longClickListener = new View.OnLongClickListener() 


    {


        @Override
        public boolean onLongClick(View v) 
        {
            for(int i=0; i<10; i++)
            {
                //Each seat[i] will correspond with each imageButtoni+1
                if(v.getId() == (getResources().getIdentifier("imageButton" + (i+1), "id", "en.deco.android.livehud"))) 
                {
                    //If the seat is empty fill it, place a player in the seat and change the button from translucent to opaque
                    if(table.seats[i].getState().equals("empty"))
                    {

                        seatButton[i].setAlpha(255);
                        //seatButton[i].setImageResource(0x7f020000);
                        table.seats[i].sit(new Player());
                        seatStats[i].setVisibility(View.VISIBLE);
                        Toast.makeText(GUI.this, table.seats[i].getState(), Toast.LENGTH_SHORT).show();
                    }
                    //If the seat is full, empty it
                    else
                    {
                        seatButton[i].setAlpha(80);
                        //seatButton[i].setImageResource(0x7f020007);
                        table.seats[i].sitOut();
                        seatStats[i].setVisibility(View.INVISIBLE);
                        Toast.makeText(GUI.this, table.seats[i].getState() + i, Toast.LENGTH_SHORT).show();
                    }
                }
            }
            return true;
        }
    };


    //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].setAlpha(80);
    }
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72

2 Answers2

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.

MH.
  • 45,303
  • 10
  • 103
  • 116
  • Thanks this is indeed an error although the problem with the first two clicks of the button still occurs. – Declan McKenna Nov 05 '11 at 22:59
  • Extended my answer with a possibly incorrect call of setAlpha. – MH. Nov 06 '11 at 01:31
  • Alpha values are ints that go from 0..255 for ImageView/ImageButtons. My last attempt has involved replacing setAlpha with an Alpha animation. Unfortunately this too failed. The first click would be fine but the second would affect all buttons. Beginning to think the Alpha methods themselves may be a little dodgy. – Declan McKenna Nov 06 '11 at 02:05
  • Ah yes, I noticed ImageView/ImageButton have their own setAlpha(int). Nonetheless, have you tried the setAlpha(float) method that is inherited from the View base class? Alternatively, you can browse through the suggestions [here](http://stackoverflow.com/questions/3262118/android-button-setalpha) and [here](http://stackoverflow.com/questions/2838757/how-to-set-opacity-alpha-for-view-in-android). – MH. Nov 06 '11 at 02:28
  • I've tried one of the above. I'll give the textView one a go later, I will have to come up with an equivalent for an imageView seeing as I'm using an imageButton. setAlpha(float) returns an error when applied to an imageButton. I found a way of solving this last night I shall also put up later. I'm not remotely happy with the solution though as it makes as little sense as the problem does. Hopefully my messy solution can help reveal what is happening here so a real solution can be found. – Declan McKenna Nov 06 '11 at 13:57
0

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);
    }
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
  • This was a temporary solution that didn't work out. I faced similar problems later were clicking completely unrelated buttons would reverse the transparency. – Declan McKenna Mar 21 '12 at 14:02