1

i am developing/designing a quiz application in android, on the testActivity i have

a TextView which loads the questions and four answer(alternatives) buttons.

btnAnswer1, btnAnswer2, btnAnswer3, and btnAnswer4.

Problem:When i click on btnAnser1 as my answer, it should remain in selected state, however it should also be possible to unselect(normal state) it if i doubt the answer. and lastly if its selected, and i decide to go for btnAnswer2 whiles btnAnswer1 is selected, immediately i select btnAnswer2, btnAnswer1 should be unselected(normal) whiles btnAnswer2 is selected.

i hope my question is clear and it sounds quiet easy but am still new in android programming so will appreciate the help.

thanks.

 optionone = (Button) findViewById(R.id.optionone);
    optionone.setTag(1);
    optionone.setId(i);
    //optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));


    optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
    optionone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            v.setSelected(true);
            checkAnswer();
            final int change = (Integer) v.getTag();
            if (change == 1) {
                optionone.setBackgroundColor(Color.parseColor("#B2B2B2"));
                back.setVisibility(View.VISIBLE);
                next.setVisibility(View.VISIBLE);
                v.setTag(0);
            } else {
                optionone.setBackgroundColor(Color.parseColor("#F1F1F1"));
                back.setVisibility(View.GONE);
                next.setVisibility(View.GONE);
                v.setTag(1);

            }

        }
    });
SoftServe
  • 67
  • 13
  • 3
    i think you can use radio button here instead going with buttons... – vinay Maneti Nov 20 '14 at 08:49
  • 1
    Please share code, so figure out problem. – Krunal Indrodiya Nov 20 '14 at 08:50
  • wel just create a global boolean `anyselected` and using the a selector drawable define selected and normal states.. and when you click a button switch `anyselected` to true and set the button view as selected = true;.. and before all this in your onclick check if `anyselected` is true then set the other buttons as selected false.. am i lucid enough? – Elltz Nov 20 '14 at 08:55
  • Refer the link http://stackoverflow.com/questions/17969925/how-to-highlight-a-button-when-is-pressed its already ans – Mourice Nov 20 '14 at 09:13
  • @Mourice with mine i want the button to stay pressed/selected untill another button is clicked then it returns to it normal state – SoftServe Nov 20 '14 at 10:50

1 Answers1

0

Try to use toggle buttons instead of buttons, or just change background color of buttons when they pressed, not state.

Avokado
  • 59
  • 1
  • 9