androidrandomtogglebuttonandroid-identifiers

How to Generate the ID of a Random Toggle Button, Then SetChecked(True) That Specific Toggle Button


I have two ToggleButtons and I want to be able to randomly select the id of one of those buttons, and set check that button to true. I tried This but is does not work as setChecked is not available for an Int or String. Any help would be much Appreciated.

int[] buttonIds = new int[] {R.id.player1Button, R.id.player2Button};
Random rand = new Random();
int num = rand.nextInt(buttonIds.length);
int buttonId = buttonIds[num];
findViewById(buttonId).toString();
String randomButton = getString(buttonId);
randomButton.setChecked(true); /// THIS LINE OF CODE WILL NOT WORK

Solution

  • Use this line instead

    ((ToggleButton)findViewById(buttonId)).setChecked(true);