Android - Highlighting of buttons - android

I have a few buttons which I want to highlight only at the borders.
That is, I want the borders of the buttons to glow with a specific color on some action taken. How do I change the border programatically?
Is it possible with drawables? How?

You can have two drawables one for selected state and one for normal state, please go
through following link:
StateListDrawable

See here: http://developer.android.com/reference/android/widget/ImageButton.html
And also here: http://groups.google.com/group/android-developers/tree/browse_frm/thread/1906f24707594f67/17322a04f7af1a5b for Romain Guy's answer:
In res/drawable, create a file called for instance mybutton_background.xml
and put something like this inside:
<?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_background_focus" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/button_background_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/button_background_pressed" />
<item android:drawable="#drawable/button_background_normal" />
</selector>
Then set this drawable as the background of your button with
android:background="#drawable/mybutton_background"

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

Listview Selector

I have a State List Drawable xml
<item android:state_pressed="true" android:drawable="#drawable/list_selector_pressed" />
<item android:state_enabled="false" android:drawable="#drawable/list_selector_disabled" />
<item android:drawable="#android:color/transparent" />
Whenever I select the row in the listview I want it to show my pressed image and then when it releases it shows the transparent background (normal). However, it always shows my disabled image after releasing. I need to have the disabled image whenever I disable a row (grey). Any ideas what i'm doing wrong? Also, is there a way to capture the different states (focused, pressed, enabled) to have a better idea what is happening behind the scenes, this might help me in understanding what is happening?
This example should be a good help.
Here's how XML should look 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/listitem_pressed" />
<item android:state_focused="true" android:drawable="#drawable/listitem_selected" />
</selector>

Using colors in a selector xml

I want to set colors for clicking instead of images. Doing so doesn't give any errors except for force-closing at runtime. Is it even possible to do this or can selector only be used for images?
Code I'm trying below:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:background="#00C0FF" />
<item android:state_focused="true" android:state_pressed="false" android:background="#0060FF" />
<item android:state_focused="false" android:state_pressed="true" android:background="#00C0FF" />
<item android:state_focused="false" android:state_pressed="false" android:background="#FFFFFF" />
</selector>
Not per the documentation
<item>
Defines a drawable to use during certain states, as described by its attributes. Must be a child of a <selector> element.
android:drawable
Drawable resource. **Required.** Reference to a drawable resource.
You can create a simple shape drawable to hold the color
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#33FF33"/>
</shape>
Is it even possible to do this or can selector only be used for images?
No, this doesn't make any sense (to me at least). Without a 'drawable', how will it know exactly 'what' to set to state_focused and state_pressed?
Why not just create some single colour drawables (bitmaps for example) and provide those as the drawables for each item?

Android - different image for rollover on ImageButton

Is it possible to specify a different image when the user's focus comes to an ImageButton? I want to display an image button on a LinearLayout and change the image when the user's focus comes on the button or when the user presses the button.
Thanks.
Yes, you can do this. What you need is a drawable xml file that defines a selector.
<selector xmlns:android...
<item android:state_enabled="false" android:state_focused="true" android:drawable="..." />
<item android:state_enabled="true" android:state_focused="false" android:drawable="..." />
</selector>
Then, use the id of this drawable XML when specifying the ImageButton in your layout XML.
The precedent answer did not work for me. Here is the code I found somewhere else:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/refresh_pushed" android:state_pressed="true" />
<item android:drawable="#drawable/refresh" />
</selector>
You can also add a state for foccussed objects by adding a line and using:
android:state_focused="true"

Categories

Resources