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:
Related
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.
This question already has answers here:
Adding custom radio buttons in android
(12 answers)
Closed 6 years ago.
I want some ImageViews to work as RadioButtons. But I don't want the mini-button of a RadioButton to select it; I want this mini-button to disappear, and the ImageView to be the entire button. Thus, a solution like the following one is not good.
<RadioButton
android:id="#+id/myId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableRight="#drawable/myIcon"/>
Is there a widget or something which I can use to use ImageViews as RadioButtons, or should I program myself all the logic of the buttons?
I will suggest to create your custom radio button. check this answer of Evan Bashir
you can try with button attribute like bellow.
<RadioButton
android:id="#+id/myId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/myIcon"/>
or through grammatically.
myRadioButton.setButtonDrawable(resourceId or Drawable);
and you can change the images while selecting the RB through the following xml file...save this file in drawable folder and call it in the button attribute or grammatically .
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/star_down" />
<item android:state_checked="false" android:drawable="#drawable/star" />
</selector>
Just set the image in the android:background of the RadioButton's XML declaration.
Also set the android:button to transparent: android:button="#android:color/transparent"
If you want to have different states - create a selector with different images for the two states.
I have some RadioButtons inside a RadioGroup and I am using custon icons to replace the typical radio "dot".
I have my normal state (checked=false) and checked state (checked=true) sorted.
However, when tapping/pressing the RadioButton, there is a grey-ish highlight colour in the background, that is round in shape (to match the original radio dot).
How can I customise this highlight colour? I tried using "android:state_pressed" but that didn't seem to do anything? (Well, I tried adding a shape, as mentioned here: https://stackoverflow.com/a/14602078/601869)
Ideally, I'd like the shape to match that of the icon (well, most probably be a bit bigger) and change the colour.
Selector 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/rounded" />
<item android:state_checked="true" android:drawable="#drawable/rounded_checked" />
</selector>
RadioButton XML:
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radioButton01"
android:button="#drawable/rounded_radio_select" />
try changing
android:button="#drawable/rounded_radio_select" />
to
android:background="#drawable/rounded_radio_select"
android:button="#android:color/transparent"/>
Maybe this is the same issue Adding custom radio buttons in android
I have a set of Radio Buttons in a RadioGroup. I have created a StateList Drawable to indicate the state of each button. The buttons operate properly in that selecting any one will kick off the listeners, etc. However, the StateList Drawable isn't working. Here's the relevant stuff:
Layout XML with button:
<RadioButton
android:id="#+id/score4"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="5dp"
android:button="#drawable/score_button_selector" />
score_button_selector xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/score_bg_2c_on" />
<item android:state_checked="false"
android:drawable="#drawable/score_bg_2c_off"/>
</selector>
score_bg_2c_off is a blue button:
and score_bg_2c_on is a green button:
The blue button appears properly, but when selected (pressed/clicked), the green one should appear instead. As I said, the operation of the button is fine, I get it's value properly, etc. - just not the drawable change. I tried state_selected instead of state_checked with no better results.
Any ideas why this isn't working as I'd like?
Thanks.
Here's some more data... I took out the android:button= and put the drawable on the android:background=.
<RadioButton
android:id="#+id/score4"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="5dp"
android:background="#drawable/score_button_selector" />
This way the default radio button shows up. This button shows when it is selected (blue inside the button), but the background never changes. I know it's seeing the score_button_selector drawable because it's showing the button with the blue background that is only defined in the drawable.
Try this
<RadioButton
android:id="#+id/score4"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="5dp"
android:button="#android:color/transparent"
android:background="#drawable/score_button_selector" />
Nothing a good project clean can't fix! I think that perhaps a majority of the time doing a clean as a last resort b-4 posting a question is a good protocol. It cleared this problem up after chasing ghosts for 3 days.
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).