Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
975 views
in Technique[技术] by (71.8m points)

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" />
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

        }
    });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...