vendredi 27 mai 2016

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/quiz_checkbox_empty"
            android:onClick="onCheckboxClicked"
            android:text="CheckBox" />




Aucun commentaire:

Enregistrer un commentaire