JB_User
JB_User

Reputation: 3267

How to set a CheckBox or RadioButton

I looked at the documentation for CheckBox and I see these two methods:

setEnabled(boolean)
setSelected(boolean)

So, in my code, I put

CheckBox myCB = (CheckBox) findViewById(R.id.CheckBox);
myCB.setEnabled(true);
myCB.setSelected(true);

When I run the code, I want the above CheckBox to already be checked and turned 'on' in the GUI. But it isn't. It looks like it's unchecked. What am I doing wrong?

Upvotes: 1

Views: 54

Answers (1)

Sky Kelsey
Sky Kelsey

Reputation: 19290

Use setChecked(boolean checked) or toggle() instead. Take a look at the javadoc for CheckBox here.

Notice that, for instance, setSelected(boolean selected) is inherited from TextView, and is used to determine the presentation of the text, not to check or uncheck the CheckBox.

Upvotes: 1

Related Questions