Unchecking radio buttons in a Radio Group without the ripple animation - android

I have a quiz app which displays the question in a textview and the 4 answers in a radio group with 4 radio buttons. To move to the next question, the user has to click a Next button.
When the user selects a particular radio button and clicks on Next button I repopulate my UI with the next question and its answers. Before displaying the next question I clear the radio button selection by using RadioGroup clearCheck().
However the user can see this selection change in the UI.
I have tried setting transparent color to the radio button background but that doesn't solve the issue.
How do I solve this?

You can end the animation by calling radioGroup.jumpDrawablesToCurrentState()
after the clearCheck().

Try adding your own radiobutton drawable like so:
quiz_radiobutton.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/checkbox_outline" />
<item android:state_checked="true" android:drawable="#drawable/checkbox_outline_checked" />
</selector>
Then, change your the drawable in your buttons:
<android.support.v7.widget.AppCompatRadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/quiz_radiobutton"
/>
This will deactivate the ripple animations.

Related

Create custom radio button

I am trying to create the attached image using radio button groups. After trying multiple combinations I am not able to achieve what is intended.
The intention is to create a circular background around the solid circle whenever one radio button is selected.
Any help would be appreciated.
The radio button images are controlled by a StateListDrawable. When a radio button is check (state_checked="true"), the drawable assigned to the checked state is shown. When the state is "not checked" (state_checked="false"), the drawable assigned to the "not checked" state is shown. Here is an example StateListDrawable:
custom_radio_button.xml
<selector>
<item android:state_checked="true" android:drawable="#drawable/ic_baseline_check_circle_24" />
<item android:state_checked="false" android:drawable="#drawable/ic_baseline_check_circle_outline_24" />
</selector>
Here, I have just selected two vector graphic files from Android Studio. You will need to create the checked/unchecked images that you want.
Now, in the XML for the layout, specify the you want to use this StateListDrawable for the radio button.
...some XML...
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_radio_button"
android:checked="true"
android:text="RadioButton 1" />
...more XML...
You would need to place this radio button and its sisters into a radio group.
Putting it all together gives the following:

How to add a click effect to a button on click event?

How would I add a click effect similar to the below image for a button click event?
The button in my activity already has it's background set to an image so I'm not sure how I would add a background of a state also as in this tutorial:
Android Button color changing on onClick?
You need two image, one for normal state and another for pressed state. At first create a selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_image" android:state_pressed="true"/>
<item android:drawable="#drawable/normal_image"/>
</selector>
Add this selector in your button as a background.
Here's something similar. I am not sure though, if this fits your solution.
EDIT: It uses selector, just like #Raghunandan suggested above.

Android RadioButton dynamic icon change

I'm doing a multiple choice quiz app. It has 4 radio buttons. What I want to have is have 3 icons -> No answer yet, right answer, wrong answer. So before any choice is clicked, all are non-answer, when an answer is clicked, if it is right, it light up green, if wrong - light up red. Is it possible to do with radio buttons? Or do I have to find another way out? Like using an ImageView?
Create one XML in "drawable" folder "radio.xml"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/radio_on" android:state_checked="true"/>
<item android:drawable="#drawable/radio_off" android:state_checked="false"/>
</selector>
Take a look On this Answer.
But u want verification for selected answer. I recognized to u that..Use ImageButton with TextView in your xml file Instead Of RadioButton.Initially Set notChecked image as background of ImageButtons .when User click on Any of your ImageButton. U can verify it On its OnClick event that is it r8 answer or not and then u can set Checked image(red/green) as background image of selected ImageButton.

Android: keep focus when other view gets it

I have 4 buttons for switching displayed content. Any time one of them is pressed, it's highlighted:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bottom_selected" android:state_pressed="true"></item>
<item android:drawable="#drawable/bottom_selected" android:state_pressed="false" android:state_focused="true"></item>
<item android:drawable="#android:color/transparent"></item>
</selector>
My problem is that I need last pressed button to keep its focus even if some other view is focused (f.e., I have EditText view, and when it's pressed, all buttons lose their focuses and none of them is highlighted). Is there any easy way to do that? Remembering last pressed button and rehighlighting it when other view is pressed doesn't sound like good solution.
UPD: It can be done by adding lastPressedButton.requestFocus() to other view's OnFocusChangeListener. Not too bright if there are many other views, maybe some other solution?
Try to add android:focusable="false" attribute to all the other fields that you don't want to be focusable.

Android Custom Image Button Does Not Stay Selected

I am currently using an ImageButton, and I want to have the effect like radio button once you select it stays selected, until someone picks another image button. I have setup custom selector like below:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/buttonimagesel" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/buttonimagesel" /> <!-- focused -->
<item android:drawable="#drawable/buttonimage" /> <!-- default -->
</selector>
But this just shows the selected image for as long as the key is pressed down. The effect i want is for it to stay selected like a radio button until the request is processed after which the whole activity including the button is redrawn. So I want one click to put the button in a selected state and unclick does not change this. Also I do not want the other buttons to be selectable after this happens, and I don't certainly don't want them to change images or anything like that.
Thanks
If you need to use an ImageButton, you can add an android:state_selected="true" item and use setSelected() in your onClick() logic. You would have to take care of deselecting all the other buttons when selecting a new one. This question might be useful: Android ImageButton with a selected state?
However you could also just use RadioButtons and customize their look (with android:background and android:button - these and all CompoundButtons have a checked state that work in a toggling way).

Categories

Resources