Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically setting state for ToggleButton in Android?
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/5041021/togglebutton-state-change-programmatically-rather-than-automatically-in-android">ToggleButton state change programmatically rather than automatically in Android?</a> </p> </blockquote> <p>I am trying to toggle images, an array with a toggle button. The only problem is I need to reset the toggle button after three rolls. I have figured out how to reset the images after 3 rolls; however, I am having to click the toggle button twice to get the state to match the images in the toggle button. I was reading <a href="https://stackoverflow.com/questions/10352133/how-to-work-with-a-togglebutton-android">this thread</a>, but when I try to apply the setActivated(false) to the loop, I get this syntax error:</p> <pre><code>The method setActivated(boolean) is undefined for the type ToggleButton </code></pre> <p>XML for the buttons:</p> <pre><code> &lt;ToggleButton android:id="@+id/tbDice1" android:layout_width="50dip" android:layout_height="50dip" android:textOn="" android:textOff="" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:background="@drawable/die_grn_6" /&gt; &lt;ToggleButton android:id="@+id/tbDice2" android:layout_width="50dip" android:layout_height="50dip" android:textOn="" android:textOff="" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:background="@drawable/die_grn_6" /&gt; &lt;ToggleButton android:id="@+id/tbDice3" android:layout_width="50dip" android:layout_height="50dip" android:textOn="" android:textOff="" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:background="@drawable/die_grn_6" /&gt; &lt;ToggleButton android:id="@+id/tbDice4" android:layout_width="50dip" android:layout_height="50dip" android:textOn="" android:textOff="" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:background="@drawable/die_grn_6" /&gt; &lt;ToggleButton android:id="@+id/tbDice5" android:layout_width="50dip" android:layout_height="50dip" android:textOn="" android:textOff="" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:background="@drawable/die_grn_6" /&gt; &lt;/TableRow&gt; </code></pre> <p>Java for the listeners:</p> <pre><code>public void playGame() { final Random rand = new Random(); dice0 = (ToggleButton)findViewById(R.id.tbDice1); dice1 = (ToggleButton)findViewById(R.id.tbDice2); dice2 = (ToggleButton)findViewById(R.id.tbDice3); dice3 = (ToggleButton)findViewById(R.id.tbDice4); dice4 = (ToggleButton)findViewById(R.id.tbDice5); txtTurnNum = (TextView)findViewById(R.id.turnNum); txtRollNum = (TextView)findViewById(R.id.rollNum); final ToggleButton[] dice = {dice0, dice1, dice2, dice3, dice4}; //array of buttons (dice) final int [] diceValue = new int [5]; final boolean [] isHeld = {false, false, false, false, false}; // array of dice to be held (hold) roll = (Button)findViewById(R.id.btnroll); score = (Button)findViewById(R.id.btnscore); roll.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v ) { rollDice(dice, diceValue, isHeld, rand); } }); score.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v ) { scoreDice(diceValue); } }); dice0.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (dice0.isChecked()) { isHeld[0] = true; String imgName = "die_red_" + diceValue[0]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice0.setBackgroundResource(id); //Changes to red } else { isHeld[0] = false; String imgName = "die_grn_" + diceValue[0]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice0.setBackgroundResource(id); //Changes to green } } }); dice1.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (dice1.isChecked()) { isHeld[1] = true; String imgName = "die_red_" + diceValue[1]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice1.setBackgroundResource(id); //Changes to red } else { isHeld[1] = false; String imgName = "die_grn_" + diceValue[1]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice1.setBackgroundResource(id); //Changes to green } } }); dice2.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (dice2.isChecked()) { isHeld[2] = true; String imgName = "die_red_" + diceValue[2]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice2.setBackgroundResource(id); //Changes to red } else { isHeld[2] = false; String imgName = "die_grn_" + diceValue[2]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice2.setBackgroundResource(id); //Changes to green } } }); dice3.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (dice3.isChecked()) { isHeld[3] = true; String imgName = "die_red_" + diceValue[3]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice3.setBackgroundResource(id); //Changes to red } else { isHeld[3] = false; String imgName = "die_grn_" + diceValue[3]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice3.setBackgroundResource(id); //Changes to green } } }); dice4.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (dice4.isChecked()) { isHeld[4] = true; String imgName = "die_red_" + diceValue[4]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice4.setBackgroundResource(id); //Changes to red } else { isHeld[4] = false; String imgName = "die_grn_" + diceValue[4]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice4.setBackgroundResource(id); //Changes to green } } }); } public int[] rollDice(ToggleButton [] dice, int [] diceValue, boolean [] isHeld, Random rand) { if(rollNum &lt; MAX_ROLLS){ for (int i = 0; i &lt; dice.length; i++) { if (!isHeld[i]) { int rndInt = rand.nextInt(6) + 1; // Random number between 1 and 6 String imgName = "die_grn_" + rndInt; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); diceValue[i] = rndInt; dice[i].setBackgroundResource(id); //Loops through the dice array and sets the appropriate dice images based on individual randoms } else { //do nothing } } rollNum++; } else { //Turn is over if(turnNum &lt; MAX_TURNS){ rollNum = 1; //reset turn number to 1 turnNum++; txtTurnNum.setText("" + turnNum); scoreDice(diceValue); updateScores(scoresArray); for (int i = 0; i &lt;dice.length; i++) { if (isHeld[i]) { isHeld[i] = false; String imgName = "die_grn_" + diceValue[i]; int id = getResources().getIdentifier(imgName, "drawable", getPackageName()); dice[i].setBackgroundResource(id); //Changes to green dice[i].setActivated(false); //PROBLEM HERE } } } else { Toast.makeText(getApplicationContext(), "Game Over", Toast.LENGTH_LONG).show(); } } txtRollNum.setText("" + rollNum); return diceValue; } </code></pre> <p>Any ideas to point me in the right direction?</p>
    singulars
    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