Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8147149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:15:15+00:00 2026-06-06T14:15:15+00:00

Possible Duplicate: ToggleButton state change programmatically rather than automatically in Android? I am trying

  • 0

Possible Duplicate:
ToggleButton state change programmatically rather than automatically in Android?

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 this thread, but when I try to apply the setActivated(false) to the loop, I get this syntax error:

The method setActivated(boolean) is undefined for the type ToggleButton

XML for the buttons:

        <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" />

        <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" />

        <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" />

        <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" />

        <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" />
        </TableRow>

Java for the listeners:

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 < MAX_ROLLS){
        for (int i = 0; i < 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 < MAX_TURNS){
            rollNum = 1;  //reset turn number to 1
            turnNum++;                  
            txtTurnNum.setText("" + turnNum);
            scoreDice(diceValue);
            updateScores(scoresArray);
            for (int i = 0; i <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;
}

Any ideas to point me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T14:15:17+00:00Added an answer on June 6, 2026 at 2:15 pm

    I ended up finding this thread, which gives me the correct syntax of button.setChecked(true)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I create a toggle button similar to the Twitter apps
Possible Duplicate: Javascript change div content upon a click What I'm trying to do
Possible Duplicate: Strange behavior Of foreach Why PHP sometimes change last element of array?
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Turning on camera flash LED in Android? I need to turn on
Possible Duplicate: Transparent images with C# WinForms I am coding an app which will
Possible Duplicate: how to use foursquare API in android application? This is a question
possible duplicate, hello all, I am not getting the reason that why ToggleButton was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.