baki1995
baki1995

Reputation: 83

Placing a Drawable to the right of a button

Im working on an app that generates multiple choice questions. When user selected a button (radio button), it will put an image of X (if wrong) or V (if right) next to the selected answer. I created RadioGroup that contains all buttons and used OnCheckedChanged Listener. I traced and put print statements to make sure it works and it does. However, it doesn't show the drawable image at all except for the first user press.

So in other words, the user will press one of the buttons, will see feedback(V or X), but then press a different button and wont see anything, although the logic DOES work (I used print statements inside the if). I use the SetCompoundDrawableWithIntrinsicBounds and it just would work. Does anybody have any idea how to solve this?

I've spent the last 8 hours working on this and im extremely frustrated.

listener code:

 @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            //get the index of the correct answer

            int indexAnswer = Integer.parseInt(myQuestion.getAnswer()) - 1;
            for (int i = 0; i < possibleAnswers.length; i++) {
                //if the button at position i was pressed
                if (checkedId == possibleAnswers[i].getId()) {
                    //if its the correct answer
                    if (i == indexAnswer) {
                        Log.i("MyAcitvity", "thats correct");
                        possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.checkmark, 0);
                    }
                    else {
                        Log.i("MyActivity", "thats wrong");
                        possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.wrong, 0);
                    }

                } else {
                    possibleAnswers[i].setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
                }
            }
        }
    });

Thanks have a great day guys.

Upvotes: 3

Views: 76

Answers (1)

masoud vali
masoud vali

Reputation: 1536

I think it's better to get the radio button first and then change it like this.

View radioButton = group.findViewById(i);
radioButton.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.checkmark, 0);

for better answer please add all your code specially those which relate to your radiogroup

Upvotes: 0

Related Questions