Issue
I'm trying to make a button stay clicked after a user chooses it. My program is a 10 question quiz where if a user chooses an option and then return to that question, it should be displayed as chosen.
I can't seem to figure out a way to display the button as clicked when switching between questions.
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();
}
});
This is an example of the onClick I have for one of the answers(I have 5 buttons for the answers). I want it so that if a user chose this answer, the button will stay clicked until they chose a different answer. I've tried some things I've seen online but nothing is working well so far.
Edit: Each question is multiple choice with 5 answers!
Solution
If a single answer is allowed to be checked among the list of answers, you need to use RadioButtons
If multiple answers are allowed to be checked, you need to use CheckBoxes or ToggleButtons
Here is a snippet of using ToggleButtons which simulates the button as 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 the 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>
colors
<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
}
}
});
Answered By - Zain
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.