Android + Change image state on button widget - android

Ok I am having a LOT of trouble finding an answer for this question. So either I'm asking it wrong or going about it the completely wrong way. Either way I could use some help. So what I'm trying to do is implement dynamic image cycling on a button widget based on state.
What I mean by this is
On Mouse Down {
Change to this image
}
On Click {
Button functionality
}
On Mouse Up {
Change back to original
}
I've seen a LOT of tutorials that place this under the click event but this isn't really what I want. I don't just simply want to change the color of the button when the button is pressed I want it to flash with the alternate image quickly and then Change back to the Original image placed on the button. How would I go about doing this?

Take a look at this link: https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
From the Link:
EXAMPLE:
XML file saved at res/drawable/button.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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
This layout XML applies the state list drawable to a Button:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />

Related

Convenient way to show touch highlight when clicking Button in Android

In iOS, one code line is enough to have button show touch highlight: yourButton.showsTouchWhenHighlighted = YES;
In Android, selector could be used. However, it's quite tedious. For example, if there're 100 button icons in my app, I have to prepare at least 200 button icons: 100 for normal and 100 for highlighted.
Is there any convenient/simpler way to do this?
You may try Ripple Effect to show touch effect
Create a single selector, and use that as a background for all your views (this doesn't need to be created for each button individually, you can apply the same one universally).
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#000000" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#000000" /> <!-- focused -->
<item android:color="#FFFFFF" /> <!-- default -->
</selector>
This can be applied universally to ALL your buttons like this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/button_selector" />
There is a new Ripple response, but this is available for Android 5.0 devices plus (there probably is a 3rd party library on GitHub allowing this to work on earlier OSes)

How to get glow on the buttons like ipod/ipad in Android

On Android application How can I get the glow effect on button when touched; I am looking for exact similar effect like ipod/iphone buttons
Use a StateListDrawable. Type this into a file and store it as res/drawable/button_bg.xml. Then create 4 images button_press.png, button_focus.png, button_disabled.png and button.png and last set your background to button_bg. If you want your button glow when touch you can add an image of your glowing button to state press. So when you press the system will get glowing_button.png and display it.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_press" android:state_pressed="true" />
<item android:drawable="#drawable/button_focus" android:state_focused="true" />
<item android:drawable="#drawable/button_disabled" android:state_enabled="false" />
<item android:drawable="#drawable/button" />
</selector>
The "magic" behind this is that android will go from the top to bottom and use the first one matching your buttons state.
In these links you can find some snippets to get exactly the same TabBar as iOS applications
iPhone Tab in Android
TabHost For Android
iTabs

How to give Flashing Effect between Two images in Android?

I have two images(Light Red and Dark Red) for the button and i want to give Flashing Effect to that button that initially it glows from light-red to Dark-red and then when it pressed, its state will be changed to dark red. i try to sort out the Solution for this but every-where i see Fade-in and Fade-out solution using one image, but i want to use both images.
Please let me know if any function exists there for giving Flashing Effect between two images or i will have to make it manually.
For this I think you have to use customized xml drawable for your button,
And for different state of button you can use different images, to apply style
Try this,
example:
XML file saved at res/drawable/button.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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
This layout XML applies the state list drawable to a Button:
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />
For more info look at Android - Drawable Style.
And nice example Adding Gradient Effects to Android Button.
SO post how to set image button backgroundimage for different state?.
You will need to work with associating Animations to Buttons.
Here are some links to tutorials :
link
video tutorial

ImageButton onclick issue

I have used a selector file for ImageButton but the image which I have defined onPress is shown for only a very short period of time. I want that it will be visible until the next window opens.
Code:
<?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/bedpres_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/bedpres_pressed" /> <!-- focused -->
<item android:drawable="#drawable/bedpres" /> <!-- default -->
</selector>
You need to use ToggleButton in that case with the selector you have.
Button is considered pressed only between the ACTION_DOWN and ACTION_UP touch event actions, so if you want your button to change picture after it's been touched, you should make it manually, calling button.setBackgroundResource(R.drawable.bedpres_pressed); Hope this helps.
After Button Cliked i think you wrote in xml file for only click
so if you want your button to change picture after it's been touched, you try to change manually, calling button.setBackgroundResource(R.drawable.bedpres_pressed);
other wise try to use Toggle Button class
hope this helps.

Android Set ImageButton as Toggle

How can I have an imagebutton stay in the 'pressed' state when it is clicked? Basically I just want the background to be the depressed background, but I can't figure out how to set it. At the moment, I've just copied the selected button background into my res folder, but when I set it as the background, it becomes blurry (since the original image is bigger than the button itself).
Normal Background:
alt text http://img707.imageshack.us/img707/9199/ss20100426163452.png
What I'm getting:
alt text http://img707.imageshack.us/img707/912/ss20100426163357.png
alt text http://img3.imageshack.us/img3/8304/ss20100426163623.png
Also I don't believe I can actually use this method considering the many different UI layouts. The button should stay pressed as per the UI the user is using.
There are a few ways of doing this:
First, you can simply use an ImageButton, and manually toggle its image drawable on click in Java. This is what the stock Music player on Android does for the shuffle button, for example. Although you won't have control over the button background in its checked state, you'll be able to swap out the image, which may be favorable from an Android UI-consistency perspective.
Another option is to use a complex set of drawables and nine-patches to get an image inside a ToggleButton, with the option of changing the background and/or the image resource upon toggle. That's the option I'll show below. But remember, be cautious about UI consistency before doing this.
res/layout/foo.xml
...
<ToggleButton
android:textOn="" android:textOff=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/shuffle_button" />
...
res/drawable/shuffle_button.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- use "#android:drawable/btn_default" to keep consistent with system -->
<item android:drawable="#drawable/toggle_button_background" />
<item android:drawable="#drawable/shuffle_button_image" />
</layer-list>
res/drawable/toggle_button_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- checked state -->
<item android:state_pressed="false" android:state_checked="true"
android:drawable="#drawable/btn_default_checked" />
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_normal_disable" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_selected" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_normal_disable_focused" />
<item android:drawable="#drawable/btn_default_normal_disable" />
</selector>
res/drawable/shuffle_button_image.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_mp_shuffle_on_btn" android:state_checked="true" />
<item android:drawable="#drawable/ic_mp_shuffle_off_btn" />
</selector>
Image files
btn_default_<state>.9.png can be found in frameworks/base.git
under
core/res/res/drawable-hdpi
and
core/res/res/drawable-mdpi (also ldpi).
WARNING: if you use these, your app will look inconsistent on devices with customized OS UIs (i.e. HTC's Sense UI).
ic_mp_shuffle_<state>_btn.9.png need to be nine-patches, so that the image gets centered and not stretched to fit the button. Below are example hdpi versions of the icon:
res/drawable-(h|m|ldpi)/ic_mp_shuffle_(on|off)_btn.9.png
Final Note: Remember to be consistent with the system UI when possible, and be mindful of the fact that your app may run on devices with customized versions of the OS that have different graphics for UI elements like buttons. An example of this is HTC Sense, which has green buttons in place of the grey/orange/yellow ones in stock Android. So, if you end up copying the btn_default_... PNG files from the open source repository to create a toggle-able button background, you'll break consistency on those devices.

Categories

Resources