Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does setAlpha() act on all my buttons whilst setImageResource() acts on just one button?
    primarykey
    data
    text
    <p>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.</p> <p>Swap: </p> <pre><code>seatButton[i].setAlpha(255); </code></pre> <p>For:</p> <pre><code>seatButton[i].setImageResource(0x7f020007) </code></pre> <p>And my code works! Only the button I press is effected. Why?</p> <pre><code>@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&lt;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 &lt; 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); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload