13

i have to develop one app.here i have to use checkbox.here i have to select checkbox means the default background color is yellow.but i wish to change the background color using gradient for checked and unchecked condition.how can i change this.please help me.

this is my current code:

 <CheckBox
   android:id="@+id/rempasswordcheckbox"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@+id/passwordview"

   android:layout_y="200dp"
   android:paddingLeft="45dp"
   android:text="Remember Password!"
   android:textColor="#1d2328" />
1
  • 1
    With material, they recommend using solid (flat) colors and not gradients. If you don't mind that, setting the android:buttonTint="@color/mybrown" is an easy way to change the checkbox color.
    – shauvik
    Commented Jun 24, 2015 at 22:52

6 Answers 6

12

if you are intersted to change the background color of the checkbox (button) use

mcheckbox.setButtonDrawable(R.drawable.someotherbackground);

where someotherbackground is an image in the drawable folder to which background you want your checkbox to be changed

try as below

 mcheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {

            if (isChecked) {

                System.out.println("checked" + isChecked);
                mcheckbox.setButtonDrawable(R.drawable.imageWhenActive);
                    System.out.println("app constant is set as "+isChecked);
            }
            else
            {
                mcheckbox.setButtonDrawable(R.drawable.imageWheninactive);
                System.out.println("app constant is set as "+isChecked);
            }

        }
    });
7

res/drawable/checkbox_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true">
        <shape>
            <gradient android:startColor="#FFFFFF" android:endColor="#000000" android:angle="-90"/>
        </shape>
    </item>
    <item>
        <shape>
            <gradient android:startColor="#000000" android:endColor="#FFFFFF" android:angle="-90"/>
        </shape>
    </item>
</selector>

In your layout:

<CheckBox ...
    android:button="@drawable/checkbox_background" />

If you want to use existing drawables:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="true" android:drawable="@drawable/checked_drawable" />
    <item android:drawable="@drawable/unchecked_drawable" />
</selector>
1
  • checkbox cannot be cast to imageview ? why this happened to me ?
    – chhameed
    Commented Oct 29, 2013 at 6:03
4

Using Code .

checkBox.setBackgroundColor(Color.BLUE);

Code

CheckBox cb = (CheckBox) findViewById(R.id.CheckBox01);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{
     @Override
     public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) 
     {
         // TODO Auto-generated method stub
         if (buttonView.isChecked()) 
         {
             //cb.setBackgroundColor(Color.BLUE);
             cb.setBackgroundColor(Color.parseColor("#FFFFFF"));
         }
         else
         {
             // Not Checked
             // Set Your Default Color. 
         }
     }
}); 
2
  • i have to use gradient color value for checked and unchecked condition.please give me solution Commented Oct 15, 2012 at 5:33
  • @user1676640 You can use Color.parseColor("#FFFFFF")
    – Chirag
    Commented Oct 15, 2012 at 5:35
4

try this code

public class MainActivity extends Activity {

CheckBox box;
boolean flag=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    box=(CheckBox)findViewById(R.id.box);

    box.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            if(flag){
                GradientDrawable d=new GradientDrawable();
                d.setColor(Color.RED);
                box.setBackgroundDrawable(d);
                }
            else{
                GradientDrawable d=new GradientDrawable();
                d.setColor(Color.GREEN);
                box.setBackgroundDrawable(d);
            }
            flag=!flag;
            }

    });
}

}

1
  • if you are using jellybean or greater sdk then use setBackground() method else use setBackgroundDrawable() Commented Mar 2, 2015 at 8:05
1

Use the following code in your checkbox xml :

<CheckBox
   android:id="@+id/rempasswordcheckbox"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_below="@+id/passwordview"

   android:background="#0000FF"

   android:layout_y="200dp"
   android:paddingLeft="45dp"
   android:text="Remember Password!"
   android:textColor="#1d2328" />
5
  • Not worked. When I put this, in this way, the button disappear.
    – Shad
    Commented Apr 14, 2014 at 18:13
  • @Shad, can you specify your exact problem? Commented Apr 15, 2014 at 1:49
  • In my case, I needed change the checkbox button background, not the whole component background. This way not worked, cause it seems the checkbox states are represented by system images. We need to do something as Yusyuriv did above: create a shape then lay color in it. Or create another images to override the original drawables.
    – Shad
    Commented Apr 22, 2014 at 13:14
  • @Shad, Your question is something different. So you should be create another question thread. Commented Apr 22, 2014 at 17:55
  • This changes a background of whole block with text, but not a little square with flag.
    – CoolMind
    Commented Aug 22, 2017 at 8:32
1

Change the colorAccent for the theme

  <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">@color/orange</item>
    ...
  </style>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.