How to make only one Button selected when clicked -QUIZ ANDROID

Viewed 2044

I am trying to implement a quiz whereby the user selects one from four buttons. When they click on the button it becomes selected, but then what I want to do is make any other button that was selected before to be reset to normal. This is my class and code, but it doesn't disable the other buttons:

public class QuizOne extends Fragment{
        Button one;
        Button two;
        Button three;
        Button four;
        boolean selected = false;

        public QuizOne(){

        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.quiz, container, false);
            one = (Button) rootView.findViewById(R.id.ones);
            two = (Button) rootView.findViewById(R.id.twos);
            three = (Button) rootView.findViewById(R.id.threes);
            four = (Button) rootView.findViewById(R.id.fours);
            selectedButton();
            return rootView;
        }

        public void selectedButton(){
            one.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(selected){
                        one.setBackgroundColor(Color.YELLOW);
                    }
                    else {
                        one.setBackgroundColor(Color.GRAY);
                        selected = true;
                    }
                }
            });
            two.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(selected){
                        two.setBackgroundColor(Color.YELLOW);
                    }
                    else {
                        two.setBackgroundColor(Color.GRAY);
                        selected = true;
                    }
                }
            });
            three.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(selected){
                        three.setBackgroundColor(Color.YELLOW);
                    }
                    else {
                        three.setBackgroundColor(Color.GRAY);
                        selected = true;
                    }
                }
            });
            four.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(selected){
                        four.setBackgroundColor(Color.YELLOW);
                    }
                    else {
                        four.setBackgroundColor(Color.GRAY);
                        selected = true;
                    }
                }
            });
        }

}

So its the selectedButton() method that I am struggling on. Because say if a user clicks on a button and then changes their mind after and selects another button then the previous button should be disabled. Also after clicking the button I want to go to the next tab

2 Answers
Related