Java – How do I keep a button selected after release?

How do I keep a button selected after release?… here is a solution to the problem.

How do I keep a button selected after release?

I’m trying to keep a button clicked after the user selects. My program is a 10-question quiz, and if the user selects an option and then returns to that question, it should show as selected.

I can’t seem to think of a way to show the button as clicked when switching between issues.

mAButton.setOnClickListener(new View.OnClickListener() {
    @Override
        public void onClick(View v) {
            Log.i(TAG, "A Button Clicked");
            answers[questionIndex] = "Question: " + questionIndex + ", Answer: A";
            Toast t;
            t = Toast.makeText(MainActivity.this, R.string.recorded_toast, Toast.LENGTH_SHORT);
            t.show();
        }
});

Here is my onClick example of one of the answers (I have 5 buttons for the answer). I want this way that if the user selects this answer, the button will be clicked until they select a different answer. I’ve tried a few things I’ve seen online, but nothing has worked so far.

EDIT: Each question is multiple choice with 5 answers!

Solution

If you allow a single answer to be selected in the answer list, you need to use RadioButtons.

If multiple answers are allowed to be ticked, you need to use CheckBoxes ToggleButtons

This is a fragment using ToggleButtons, which simulates a button being clicked

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />

<ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />

<ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />

<ToggleButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/select"
        android:textOff="Unselected"
        android:textOn="Selected" />
</LinearLayout>

And create a drawable for background selection

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/checked_button" android:state_checked="true" />
    <item android:drawable="@color/unchecked_button" android:state_checked="false" />
</selector>

Color

<resources>
    <?xml version="1.0" encoding="utf-8"?>
    ...
    <color name="checked_button">#989898</color>
    <color name="unchecked_button">#f8f8f8</color>

</resources>

Java:

ToggleButton button1 = findViewById(R.id.btn1);
ToggleButton button2 = findViewById(R.id.btn2);
ToggleButton button3 = findViewById(R.id.btn3);
ToggleButton button4 = findViewById(R.id.btn4);

button1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
             Button1 is checked
        } else {
             Button1 is unchecked
        }
    }
});

enter image description here

Related Problems and Solutions