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
Related
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.
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/
I want to create "button" in XML layout (if possible) which has background image based on state (this image is NOT 9-patched) and with text. Which component to use?
My button_states.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/my_pressed" />
<item android:drawable="#drawable/my_normal" />
</selector>
my_pressed and my_normal are NON 9-patched images.
Now if I use regular button
<Button
android:id="#+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_states"
android:text="#string/my_text" />
button it is rendered without background image.
How to do "button" with text and stateful "static" backgrounds?
Thx
Regards
The easiest way to accomplish this is doing it in code. Use setBackgroundDrawable() on the (regular) Button to set the desired background. If you use your selector as the provided drawable you should be fine.
[Edit] To do this using resources, place your button_state.xml in the res\drawable folder in use the following code:
Button button = (Button) findViewById(R.id.my_button);
button.setBackgroundResource(R.drawable.button_state);
I have a homescreen widget with an imagebutton. I have the button working with a pending intent, but I can't seem to figure out how to change the button image when it is pressed.
I tried using a selector and it works in my widget test activity, but not in the remoteview.
How could I implement this functionality in the home screen widget?
You can set a different Image within your remote view.
remoteView.setInt(R.id.buttonimg, "setImageResource", R.drawable.button_on);
I was able to get it working by moving the selector inside it's own XML file and putting in res/drawable and then referencing that xml file as my resource for my imagebutton in the app-widget's layout xml.
buttonimg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="#drawable/button_off" />
<item
android:state_pressed="true"
android:drawable="#drawable/button_on" />
<item
android:drawable="#drawable/button_off" />
</selector>
widget_layout.xml
<ImageButton
android:id="#+id/buttonimg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="0px"
android:layout_y="2px"
android:clickable="true"
android:src="#drawable/button"
>
I now have a different problem, however. The selector only allows me to change the image onPress. On release, the image changes back >:-(
How do I get the image state change on press and stay in that state until next press (like the ToggleButton)?