Reputation: 1
I want use a custom checkbox in my application android and this custom checkbox do not have a function to set color and in the example they set color on xml using (http://schemas.android.com/apk/res-auto) like (app) and set color that way app:stroke_color="#2196F3" I would like to know how set color programmatically , link for custom checkbox https://github.com/lguipeng/AnimCheckBox
Upvotes: 0
Views: 59
Reputation: 1944
I looked at the library. They do not have a setter
for stroke_color
. The only workaround here is to use Reflection
to access this private field directly. Reflection is almost always a bad practice in java. But if you really want to do it, you can do it this way:
AnimCheckBox checkbox = (AnimCheckBox)findViewById(R.id.checkbox);
try{
Field field = checkbox.getClass().getDeclaredField("mStrokeColor");
field.setAccessible(true);
field.setInt(checkbox,Color.parseColor("#2196F3"));
checkbox.invalidate();
}
catch (NoSuchFieldException e){
e.printStackTrace();
}
catch (IllegalAccessException e){
e.printStackTrace();
}
You can set the circle color in the same way. For circleColor, the field name is mCircleColor
.
Upvotes: 1