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.
By default, the android CheckBox will be in the OFF (Unchecked) state. We can change the default state of CheckBox by using android:checked attribute. In case, if we want to change the state of CheckBox to ON (Checked), then we need to set android:checked = “true” in our XML layout file.
If you want to change checkbox color then "colorAccent" attribute will use for checked state and "android:textColorSecondary" will use for unchecking state. "actionOverflowButtonStyle" will use for change the color of overflow icon in the Action bar. Same is for refresh button which i am using in my app.
To define the click event handler for a checkbox, add the android:onClick attribute to the <CheckBox> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.
You can set style to view programmatically with ContextThemeWrapper.
CheckBox mCheckBox = new CheckBox(new ContextThemeWrapper(context, R.style.MyCheckBox));
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With