Yuri
Yuri

Reputation: 4508

Programmatically styling Android's CheckBox

I got a class which extends Activity and programmatically adding some View items.

CheckBox checkBox = new CheckBox(this, null, android.R.style.Widget_CompoundButton_Star);

I'd like to set some checkBoxes styled as the Android SDK stars. I can't see any check box after I set its style to that Widget_CompoundButton_Star. Although that SDK style apparently works when directly placing in a xml.

Am I missing something here? THX

// As Pragnani said. Using android.R.attr.starStyle instead works.

Upvotes: 5

Views: 9605

Answers (3)

dkero
dkero

Reputation: 121

This is my working solution:

CheckBox checkBox = new CheckBox(new ContextThemeWrapper(this, R.style.CheckBoxStyle))

Upvotes: 0

Roman Smoliar
Roman Smoliar

Reputation: 1133

You can set style to view programmatically with ContextThemeWrapper.

CheckBox mCheckBox = new CheckBox(new ContextThemeWrapper(context, R.style.MyCheckBox));

Upvotes: 11

Roc Boronat
Roc Boronat

Reputation: 12171

I've arrived here looking for styling the text of the CheckBox instead of the Drawable, and I find the solution by another way. But hey! Let me share the solution to the myself of the future.

Instead of:

CheckBox cb = new CheckBox(this, null, R.style.yourstyle);

Use:

CheckBox cb = new CheckBox(this);
cb.setTextAppearance(this, R.style.yourstyle);

Hope it helps to solve the topic of the question!

Upvotes: 5

Related Questions