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.
Related
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:
Can I remove border of RadioButton and I have RadioButtons like bellow :
I see a lot of question and I used from android:buttonTint="" and style ... but don't work .
You can't remove that using the radiobutton.
what you can do is set it to the same color as your background,
or create a custom 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/your_radio_off_image_name" />
<item android:state_checked="true"
android:drawable="#drawable/your_radio_on_image_name" />
</selector>
use that XML in your layout:
<RadioButton
android:id="#+id/radiobutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_radiobutton"/>
more info and examples about custom xml:
Adding custom radio buttons in android
remove default radio button checkbox when using a custom selector
Another option is to use a toggle instead of radiobutton, and just change the drawable onToggle.
You will need to create a custom radio button to change the style in that way. Read this article if you want to learn how to do it. Good luck.
No, you can't remove this.You can custom radio button.
I need to make a rounded rectangular toggle_switch in android like the one given below:
Can anyone guide me the complete steps to do so.
I solved my problem as follow :
Added a toggle button to my xml layout file :
<ToggleButton
android:id="#+id/ToggleButton1"
android:layout_width="120dp"
android:layout_height="25dp"
android:layout_marginRight="30dp"
android:layout_weight="2"
android:background="#drawable/toogle_switch"
android:text="ToggleButton"
android:textOff=""
android:textOn="" />
Then defined a custom togglebutton Background "toogle_switch" in 'drawable' folder as below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/switchon" android:state_checked="true"></item>
<item android:drawable="#drawable/switchoff" android:state_checked="false"></item>
</selector>
switchon & switchoff are the 2 images I have shown in question.
Hope it helps everyone.! :)
Here you go:
http://developer.android.com/guide/topics/ui/controls/togglebutton.html
Exact image shown is determined by so called 'selector' or 'state list', which is a piece of XML that maps button states to images.
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
You need to:
prepare images for all possible states of the button (toggled, pressed, etc) and place them in drawable folders
write a state list (selector) that binds images with button states
connect this state list to button android:background property
hi i have to realize this layout . it has this layout.
I could try to use the icons as imagebuttons but the active state of a button is somewhat like this one !
How should i proceed with this ?
You should use selector as follows:
Prepare 2 images for button states, and put it into res/drawable folder.
button_normal_green.png – Default image button.
button_pressed_yellow.png – Display when button is pressed.
Now, create a new XML file in “res/drawable/” folder, in whatever name you want, in this case, we just give a name as “new_button.xml“. This file defined which button state is belong to which image.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow" android:state_pressed="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
3.set background to button
<ImageButton
android:id="#+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/new_button" />
Have a look at Complete Example
So I have an image button and I want to have two states on it. It will work like a bookmark button which has two states. If the user presses the button then it changes the image and if it re-presses the button then I want the original image back.
Is that possible? Is there an easier way using another type of button?
Thank you
Try changing the topic to :"What kind of Button can toggle/change states?"
Seems like you need ToggleBotton
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="26dp"
android:background="#color/button_colors"
android:button="#null"
android:textOff="#null"
android:textOn="#null" />
And this xml defines the colors/images of the button at rest/pressed states, put it in res/color/button_colors.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/button_rest"/>
<item android:state_checked="true" android:drawable="#drawable/button_on" />
</selector>
Yes, you can use regular buttons also. First make a selector XML file in the res/drawable directory. Example:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_selected="true"
android:drawable="#drawable/selectedimage"/>
<item android:state_enabled="true" android:state_selected="false"
android:drawable="#drawable/notselectedimage"/>
</selector>
In the button definition in the layout xml, you just add this attribute:
android:background="#drawable/myselectorxmlfile"
And then you programmatically set the selected states yourself (in a onClickListener) and the button images will change background state.
button.setSelected(true) // or false;
These links might help you..
http://developer.android.com/resources/tutorials/views/hello-formstuff.html
and
http://developer.android.com/reference/android/widget/ToggleButton.html
I guess you can use Toggle Button and hope it'll solve your issue.
Just have a look at a simple xml code of it:
<ToggleButton
android:id="#+id/tglbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF"
/>
And Just check the answer given here in this link:
Android: Specify two different images for togglebutton using XML
Also,
I've posted a simple tutorial about Toggle Button. Just have a look at my humble blog if you find it helpful. Here is link: http://androiddesk.wordpress.com/2012/03/10/toggle-button-in-android/