I created RadioButton
and CheckBox
in LinearLayout
programatically. But, now I want to change radio button\'s color and check boxes\'s
I am continuing the answer of ywwynm.
Google made the setSupportButtonTintList restricted so it is not possible to use it.
The workaround is to use the button as a TintableCompoundButton interface, in which the method is not restricted.
Works in API 19+ for AppCompatRadioButton. AppCompatCheckbox implements the same interface so it should work as well in theory but I haven't tested it.
Have fun :)
public static void setAppCompatRadioButtonColor(AppCompatRadioButton radioButton, int uncheckedColor, int checkedColor) {
ColorStateList colorStateList = new ColorStateList(
new int[][] {
new int[] { -android.R.attr.state_checked }, // unchecked
new int[] { android.R.attr.state_checked } // checked
},
new int[] {
uncheckedColor,
checkedColor
}
);
((TintableCompoundButton) radioButton).setSupportButtonTintList(colorStateList);
}
Try this
AppCompatRadioButton newRadioButton = new AppCompatRadioButton(this);
AppCompatCheckBox newCheckBox = new AppCompatCheckBox(this);
Insted of
RadioGroup newRadioButton = new RadioGroup(this);
CheckBox newCheckBox = new CheckBox(this);
You can create dynamic RadioButton
like this:
RadioButton male = new RadioButton(ReservationContact.this);
male.setTextColor(Color.BLACK);
male.setText("Male");
male.setSelected(true);
male.setId(0);
This is used for changing default color of circle of RadioButton
.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
male.setButtonTintList(ColorStateList.valueOf(ContextCompat.getColor(ReservationContact.this, R.color.background)));
}
male.setHighlightColor(getResources().getColor(R.color.background));
Use AppCompatCheckBox and AppCompatRadioButton instead of CheckBox and RadioButton. Your xml will have :
<android.support.v7.widget.AppCompatCheckBox
android:id="@+id/cbSelected"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/colorAccent" //This to set your default button color
android:checked="true"/>
<android.support.v7.widget.AppCompatRadioButton
android:id="@+id/rb"
app:buttonTint="@color/colorAccent" //This to set your default button color
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Now for the java code : Create ColorStateList
ColorStateList colorStateList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_enabled} //enabled
},
new int[] {getResources().getColor(R.color.colorPrimary) }
);
To change the color programatically for AppCompatRadioButton or AppCompatCheckBox call setSupportButtonTintList.
AppCompatRadioButton radioButton = (AppCompatRadioButton) findViewById(R.id.rb);
radioButton.setSupportButtonTintList(colorStateList);
AppCompatCheckBox cbSelected = (AppCompatCheckBox) findViewById(R.id.cbSelected);
cbSelected.setSupportButtonTintList(colorStateList);
RadioButton rb=(RadioButton) findViewById(R.id.radioButton1);
rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
rb.setButtonDrawable(R.drawable.image);
rb.setHighlightColor(Color.parseColor("#0c83bd"));
}
});
}
For CheckBox
, replace your CheckBox
with AppCompatCheckBox
and call following method:
public static void setCheckBoxColor(AppCompatCheckBox checkBox, int uncheckedColor, int checkedColor) {
ColorStateList colorStateList = new ColorStateList(
new int[][] {
new int[] { -android.R.attr.state_checked }, // unchecked
new int[] { android.R.attr.state_checked } // checked
},
new int[] {
uncheckedColor,
checkedColor
}
);
checkBox.setSupportButtonTintList(colorStateList);
}
I think it should be similar for RadioButton
. You can check declared attributes in android.R.attr
and change the code.