aarti ladva
aarti ladva

Reputation: 69

How can I change Checkbox color when I click and unclick?

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

Answers (5)

Darshi Patel
Darshi Patel

Reputation: 294

try this:
mSelectedItem = -1;
holder.country.setBackgroundResource(mSelectedItem == position ? 
R.color.lightgreen: R.color.btnbckgrd);

Upvotes: 1

MurugananthamS
MurugananthamS

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
   }

refer this link another method

Upvotes: 0

Antonis Radz
Antonis Radz

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

Ghoul
Ghoul

Reputation: 46

Have you tried android:tint="@color/your_color" in your layout file??

Upvotes: 0

akshay_shahane
akshay_shahane

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

Related Questions