Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change checkbox background color in android

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" />
like image 238
user1676640 Avatar asked Oct 15 '12 05:10

user1676640


People also ask

How can I change checkbox background in Android?

This example demonstrates how do I change the color of the check box in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I change the default checkbox color in Android?

If you want to change checkbox color then "colorAccent" attribute will use for checked state and "android:textColorSecondary" will use for unchecking state. "actionOverflowButtonStyle" will use for change the color of overflow icon in the Action bar. Same is for refresh button which i am using in my app.

How do I change the color of a checkbox?

“:hover” is used to style the checkbox when user hovers over it. Notice that when the mouse pointer move over the checkbox the color of it changes to yellow. “:active” is used to style the checkbox when it is active. Notice that when click the checkbox it will first notice a red color and then the green color.


6 Answers

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);
            }

        }
    });
like image 149
G_S Avatar answered Oct 04 '22 17:10

G_S


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>
like image 20
Yusyuriv Avatar answered Oct 04 '22 17:10

Yusyuriv


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. 
         }
     }
}); 
like image 44
Chirag Avatar answered Oct 02 '22 17:10

Chirag


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;
            }

    });
}

}

like image 24
Android Developer Avatar answered Oct 03 '22 17:10

Android Developer


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" />
like image 23
Android Boy Avatar answered Oct 05 '22 17:10

Android Boy


Change the colorAccent for the theme

  <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">@color/orange</item>
    ...
  </style>
like image 41
Riga Avatar answered Oct 01 '22 17:10

Riga