How can add imagebutton click effect? - android

I am working with android. I want to add onClick() effect on ImageButton preferably a color around the sides of the button after selected. I had tried with alpha effect.But It looks dark even after get back after click.
How can add ImageButton click effect?

you can use a selector xml like as follows :-
<?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_focused_orange"
android:state_focused="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
and set it as a background of your Button.

Related

Button change color

I have two buttons in layout,let suppose button A and button B and I want that when user touch any of two button,their background color should change for that moment.
code
``
<item android:state_hovered="true"
android:drawable="#drawable/state_hovered"/>
<item android:state_pressed="true"
android:drawable="#drawable/state_pressed"/>
<item android:drawable="#drawable/state_deafult" />
``
State_pressed working...but state_hovered is not working.
So,please suggest a way to do so.
Thanks in advance.
Change color while button is pressed?
See an answer to this question, asked previosly in StackOverflow:
Create a my_button_background.xml file in drawable folder:
<?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/blue" />
<item android:state_focused="true" android:drawable="#color/gold" />
<item android:drawable="#color/grey" />
</selector>
And use this in layout file:
android:background="#drawable/my_button_background"
And read more about Android's Color State. Basically, you can assign colors, drawables, shapes, etc. to different states of views, such as enabled, disabled, focused, clicked, etc.

How to create android button animation in proper way

I want to create a button animation like nexus back button in below image.
When it's clicked, it's highlighted with oval shape and then bending the transparency of background color smoothly.
Approximately similar effect you can achive by selector like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime" >
<item android:state_pressed="false" android:state_focused="true" android:drawable="#color/red" />
<item android:state_pressed="true" android:drawable="#color/dark_red" />
<item android:drawable="#color/red" />
</selector>
add this selector as bg_selector.xml in your res/draweble folder.
And then set it is as background of TextView(or other View):
android:background="#drawable/bg_selector"
P.S: All magic in android:exitFadeDuration

How can I change the highlight color of a borderless button?

An ImageButton using the borderlessButtonStyle has the holo blue highlight when tapped on it. How can I change the highlight color?
(I know how to change the holo color on regular buttons and image buttons using http://android-holo-colors.com/)
thanks!
button_background.xml
<?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_checked="false" />
<item android:drawable="#drawable/button_focused_orange"
android:state_checked="true" />
<item android:drawable="#drawable/button_focused_orange"
android:state_focused="true"></item>
</selector>
copy this in drawable folder. And set the item drawables according to you. Then set it as
background for your button in layout xml.

Making a button look pressed in

I'm using Eclipse to write the android application. I've added some standard buttons from the Form Widgets tab and successfully got them opening new windows which display additional buttons.
I would like the button that was pressed, to change appearance and continue to look pressed in after it is selected.
create xml file using the button image like this
<?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/transparent" />
<item
android:drawable="#drawable/closebutton" />
</selector>
add the color folder in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="transparent">#00000000</color>
</resources>
can use selector
<?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/button_settings" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/button_settings" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/button_settings_selected" />
<item android:drawable="#drawable/button_settings" />
</selector>
now set this drawable on background property of button in XML, now in coding take a boolean flag when button is pressed set the flag and set the bacground of the button(selected image) and on again click reset flag value and change the imageBackground to the selector again, thats it!!!
Use ImageButton and change the background of the button.
you can use this kind of images depends of you need. Like this https://web.archive.org/web/20160429124711/http://www.devahead.com/blog/2011/08/creating-a-custom-android-button-with-a-resizable-skin/

On click effect on the image button

I have created image buttons instead of normal buttons.On click on the image button I want a click effect as in the normal icon click effect(On click of menu the background of the icon will be yellow color) of android phones.Using the following code I code show an image on click of the image button .What changes I have to make further.
<item android:state_enabled="true"
android:state_pressed="true"
android:drawable="#drawable/production_order" />
android:state_enabled="false"
android:drawable="#drawable/prodution_orders_icon" />
Try this code
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/camera" />
<item android:state_pressed="false" android:drawable="#drawable/camera" />
<item android:state_pressed="true" android:drawable="#drawable/camera_active" />
</selector>
Instead of using directly ImageButton you can use Button with customized background
Here is the sample tutorial.
create xml file using the button image like this
<?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/transparent" />
<item
android:drawable="#drawable/closebutton" />
</selector>
add the color folder in values
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#ffffff00</color>
</resources>

Categories

Resources