Schwesi
Schwesi

Reputation: 4904

Recognize second click event in android

I am programming my first android app. It is a quiz app, in which a question with 4 answer options is displayed.

I would like to display the correct answer as soon as one of the buttons is clicked and then display the next question when the button is clicked again.


At the moment the correct answer and the next question are displayed at the same time.

public class ACEInhibitors extends Fragment implements View.OnClickListener {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button checkBtn;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /** Inflating the layout for this fragment **/
    View view = inflater.inflate(R.layout.question, null);

    DatabaseHelper db = new DatabaseHelper(getActivity());

    quesList = db.getAllACEQuestions();
    currentQ = quesList.get(qid);
    txtQuestion = (TextView) view.findViewById(R.id.textView);
    rda = (RadioButton) view.findViewById(R.id.radio0);
    rdb = (RadioButton) view.findViewById(R.id.radio1);
    rdc = (RadioButton) view.findViewById(R.id.radio2);
    rdd = (RadioButton) view.findViewById(R.id.radio3);

    rda.setOnClickListener(this);
    rdb.setOnClickListener(this);
    rdc.setOnClickListener(this);
    rdd.setOnClickListener(this);

    setQuestionView();

    return view;
}

@Override
public void onClick(View view) {
    Fragment fragment = null;
    switch (view.getId()) {
        case R.id.radio0:
            // Happening for the first click
            // Set colors according to correct answer
            rda.setBackgroundColor(Color.RED);
            rdb.setBackgroundColor(Color.RED);
            rdc.setBackgroundColor(Color.RED);
            rdd.setBackgroundColor(Color.RED);

            if (currentQ.getANSWER().equals(currentQ.getOPTA())) {
                rda.setBackgroundColor(Color.GREEN);
            } else if (currentQ.getANSWER().equals(currentQ.getOPTB())) {
                rdb.setBackgroundColor(Color.GREEN);
            } else if (currentQ.getANSWER().equals(currentQ.getOPTC())) {
                rdc.setBackgroundColor(Color.GREEN);
            } else if (currentQ.getANSWER().equals(currentQ.getOPTD())) {
                rdd.setBackgroundColor(Color.GREEN);
            }

            // Supposed to happen for the second click
            // Display the next question
            currentQ = quesList.get(qid);
            setQuestionView();
            break;
    }
}

private void setQuestionView() {
    txtQuestion.setText(currentQ.getQUESTION());
    rda.setText(currentQ.getOPTA());
    rdb.setText(currentQ.getOPTB());
    rdc.setText(currentQ.getOPTC());
    rdd.setText(currentQ.getOPTD());
    qid++;
}

public void replaceFragment(Fragment newFragment) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.fragment_container, newFragment);
    transaction.commit();
}

}


I am really new to android and java programming and would be very thankful for any kind of help!

Upvotes: 0

Views: 121

Answers (1)

Sanjeet
Sanjeet

Reputation: 2403

Do below steps to achieve desired results

  1. Add one button initially with visibility gone.

  2. Now replace setQuestionView() call from onClick with button.setVisibility(View.VISIBLE);

  3. Add click listener to button.

  4. On button click call setQuestionView() again.

Upvotes: 1

Related Questions