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/
Related
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
(Now I have come across related questions on StackOverflow but unfortunately none of the solutions worked for me which is why I had to ask this separately)
I am a Novice to Android. The problem: I need to have an image that acts as a button. Now I understand that this can be achieved by either using an image on a standard button or by using something called as "ImageButton" (which I guess is highly recommended although I have no idea why).
Requirements: I need detailed guidance for going about this problem. Specifically, I thought the "putting-image-on-standard-button" was easier until I faced two major issues: I was unable to set the image in the center (thanks drawable-top,bottom,left6,right) and once I changed the background color to match that of the Activity screen's back-color, the button effect disappeared. Simply put, I need a moderately easy way of having an image act as a button or a button with an image which has all three effects: focussed, pressed and default. I used ImageButton but then I did not know how to make custom shapes or 9patch images to give it all the desired effects, I am pretty satisfied with the default button that android provided. All I simply need is something like a background hover over the image or something of that sort which indicates the user that the image is being pressed and the event has been generated!
Can someone please help me out with this? I need the UI to look decent and therefore, need the corresponding effects on my image/button. Thanks in Advance :)
This is my icon-image:
I wish to have some sort of hover effect around this that indicates that the image has been pressed just like any normal button.
Use ImageButton and StateList Drawable. You need selector for different button's states. You can assign different drawable for different state to imitate the onFocus or onPressed effect on normal button.
This is selector.xml in drawable folder under res:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/cyan"/> <!-- pressed state -->
<item android:state_focused="true"
android:drawable="#color/cyan"/> <!-- focused state -->
<item android:drawable="#android:color/transparent"/> <!-- default state -->
</selector>
And this is color.xml in values folder under res:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="cyan">#33B5E5</color>
</resources>
Set the ImageButton's src to your image and set the background to selector.xml.
This is the final result:
There is a good tutorial here: Android ImageButton Selector Example
If someone also still has an issue with this.
I've created the selector but referred the drawable to two different image files, and used the XML in the imagebutton as a source. It worked like a charm.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_add_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/btn_add"
android:state_focused="true" />
<item android:drawable="#drawable/btn_add" />
</selector>
And the image button looks like this:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/add_button_selector"
android:background="#null"/>
create xml view
<ImageView
android:id="#+id/imageview1"
android:background="#drawable/selector_xml_name"
android:layout_width="200dp"
android:layout_height="126dp"/>
create inside draw able folder selector_xml_name.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected"android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
create inside draw able folder numpad_button_bg_selected.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
<solid android:color="#color/selected"/>
<padding />
<stroke android:color="#000" android:width="1dp"/>
<corners android:bottomRightRadius="15dp" android:bottomLeftRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp"/>
this should be an easy one and I really thought that what I was doing was correct but apparently it isn't. I have an image that I want to use as a button, I have the button created and working. But now I just want this image to show up instead of the standatd android "button".
Anyone know how to do this?
Make a use of <selector
Create in your drawable-hdpi folder xml file called button_image.xml
and add the following lines:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/picture" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/picture_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/picture_pressed" />
<item android:drawable="#drawable/picture" />
</selector>
and then in your main xml where you declare your button add this line
android:background="#drawable/button_image"
And then you get effect of reall button. Note that you need two pictures to get effect of button being clicked.
If you dont want to use the selector and use just picture instead then just
android:background="#drawable/myimage"
Use ImageButton class.
You can Use ImageButtons instead of regular buttons
The just set your background from your image in your XML
<ImageButton android:background="#drawable/mypicture"/>
A nice tutorial is Here
Follow me, You can use many controls in android to solve this problem (Button, ImageView, ImageButton, or even TextView ... ). Just sets an image as its background and then implements OnClickListener to handle click event.
Example, with ImageView,
- in layout file:
<ImageView
android:id="#+id/button_login"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#drawable/button_login"
/>
in behind-code file:
ImageView login;
login = (ImageView) findViewById(R.id.button_login);
login.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//TODO: your code here
}
});
You need to write the android:background="#drawable/my_pic" to your xml file at the button. Where my_pic is your pic's name. Hope it helps you.
I would like to know if we can customise a button to show like the Apple iOS button in Android?
The button that I would like to draw is
If this is possible, can someone give me the pointers to it.
Thanks.
Yes, it can be done. I'll leave the philosophy to others. The button background can be replaced with anything you choose. I saved your back icon above as iphoneback_normal.png.In photoshop I created two more states--focused and pressed
<Button
android:id="#+id/buttonNoThx3"
android:text="Back"
android:layout_height="fill_parent"
android:background="#drawable/iphonebackbutton"
/>
Create a state list iphonebackbutton drawable xml:
<?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/iphoneback_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/iphoneback_focused" />
<item android:drawable="#drawable/iphoneback_normal" />
</selector>
You should take a look at 9-patch png.
But using iOS-oriented approach on Android may result in poor User Experience.