Reputation: 69
When I click on checkbox color change of that checkbox and rest of check box remain another color so how can I change the color of checkbox ?
if (CountryList.get(pos).getSelected()) {
CountryList.get(pos).setSelected(false);
holder.country.setBackgroundResource(R.color.btnbckgrd);
} else {
CountryList.get(pos).setSelected(true);
holder.country.setBackgroundResource(R.color.lightgreen);
}
Upvotes: 2
Views: 220
Reputation: 294
try this:
mSelectedItem = -1;
holder.country.setBackgroundResource(mSelectedItem == position ?
R.color.lightgreen: R.color.btnbckgrd);
Upvotes: 1
Reputation: 2405
try this:
final CheckBox cbox= (CheckBox) findViewById(R.id.cb_flash);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Set the Flash CheckBox Text Color
cbox.setTextColor(Color.BLUE);
// Set the Flash CheckBox Background Color
cbox.setBackgroundColor(Color.parseColor("#cbff75"));
}
});
if(checkbox.isChecked()){
cbox.setTextColor(Color.BLUE);
// Set the Flash CheckBox Background Color
cbox.setBackgroundColor(Color.parseColor("#cbff75"));
}else{
//chenge color
}
Upvotes: 0
Reputation: 3097
Correct answer which is working on all API would be:
Create style:
<style name="CheckBox">
<item name="colorControlNormal">@color/light_grey</item>
<item name="colorControlActivated">@color/ts_white</item>
</style>
and use it like this:
<androidx.appcompat.widget.AppCompatCheckBox
.....
android:theme="@style/Bonus.CheckBox"
..... />
after setting style you wont going to need to check views background programmaticaly
Upvotes: 0
Reputation: 4633
you can use style for this
<style name="yourStyle" parent="Base.Theme.AppCompat">
<item name="colorAccent">your_color</item> <!-- for uncheck state -->
<item name="android:textColorSecondary">your color</item> <!-- for check state -->
</style>
set it in xml like this
<CheckBox
android:theme="@style/yourStyle"
/>
Upvotes: 0