saim saim - 6 months ago 18
Android Question

How to Change checkBox button icon in Android?

i want change checkBox Button icon/image when i checked checkboxes according to given condition. Here i am using MSQS i want if ANSWER Correct checked then change image of textbox, similarly when Answer check false then also changed image , i have save images in drawable please anyone tell me how it is possible.
When Checkbox checked once then it should not be rechecked again or unchecked. Thanx in advance who will help me.

public void onCheckboxClicked(View view) {

switch(view.getId()) {

case R.id.chk_ans1ID:
ans_2.setChecked(false);
ans_3.setChecked(false);
ans_4.setChecked(false);


ans_2.setEnabled(false);
ans_3.setEnabled(false);
ans_4.setEnabled(false);

if(QuizActivity.op1.equalsIgnoreCase(QuizActivity.ans))
{
correct++;


} else if (QuizActivity.op2.equalsIgnoreCase(QuizActivity.ans)) {
ans_2.setChecked(true);
incorrect++;
}else if (QuizActivity.op3.equalsIgnoreCase(QuizActivity.ans)) {
ans_3.setChecked(true);
incorrect++;
}else if (QuizActivity.op4.equalsIgnoreCase(QuizActivity.ans)) {
ans_4.setChecked(true);
incorrect++;
}else{
Toast.makeText(QuizActivity.this, "No Answer Match!", Toast.LENGTH_LONG).show();
Toast.makeText(QuizActivity.this, "answer= "+ans, Toast.LENGTH_LONG).show();

}
break;

case R.id.chk_ans2ID:
ans_1.setChecked(false);
ans_3.setChecked(false);
ans_4.setChecked(false);

ans_1.setEnabled(false);
ans_3.setEnabled(false);
ans_4.setEnabled(false);
if(QuizActivity.op2.equalsIgnoreCase(QuizActivity.ans)){
correct++;

}else if (QuizActivity.op1.equalsIgnoreCase(QuizActivity.ans)) {
ans_1.setChecked(true);
incorrect++;
}else if (QuizActivity.op3.equalsIgnoreCase(QuizActivity.ans)) {
ans_3.setChecked(true);
incorrect++;
}else if (QuizActivity.op4.equalsIgnoreCase(QuizActivity.ans)) {
ans_4.setChecked(true);
incorrect++;
}else{
Toast.makeText(QuizActivity.this, "No Answer Match!", Toast.LENGTH_LONG).show();
Toast.makeText(QuizActivity.this, "answer= "+ans, Toast.LENGTH_LONG).show();

}
break;


}
}


here is my xml of check box

<CheckBox
android:id="@+id/chk_ans4ID"
android:layout_width="match_parent"
android:layout_height="64dp"
android:button="@drawable/custom_checkbox"
android:onClick="onCheckboxClicked"
android:text="CheckBox" />


Here is my custom_checkbox.XML

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/checkbox_inactive"
android:state_checked="false"/>
<item android:drawable="@drawable/checkbox_active"
android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_inactive"/>

</selector>


It is working for me great on emulator, but when i use this app on Android mobile then text overwrite on checkbox button icon how i can prevent it?

Answer

Try this , Make this xml your drawable folder

custom_checkbox.xml

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/checkbox_inactive" android:state_checked="false"/>
<item android:drawable="@drawable/checkbox_active" android:state_checked="true"/>
<item android:drawable="@drawable/checkbox_inactive"/>

</selector>

and used this for programatically add drawable

yourcheckbox.setButtonDrawable(R.drawable.custom_checkbox);

and used this for xml

android:button="@drawable/custom_checkbox"