How to use a selector to change an ImageButton image? - android

How do I make an ImageButton image change its image when you press it?

I hope you find this helpful.
This can all be done in the XML.
1) Import your images for both pressed and unpressed states into the res/drawable-whichever folder
2) Make your selectors. Right click on a drawable folder and select New/Android xml file. Put in the name eg "ok_button_selector.xml" and choose "selector" as the root element from the menu below. You will need to create a different selector for each button on the screen.
3) In each selector file you need to define the image that will display when clicked, like this:
<!-- language: lang-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/search_icon_pressed"/>
<item
android:drawable="#drawable/search_icon"/>
</selector>
They have to be in this order as the last is the default.
4) In your layout file use the android:onClick="myButtonClicked" method to define the buttons clicked behaviour. This saves having to use click listeners. Just make sure your java method has the same name :-)
5) Within the ImageButton tags define the attribute android:src="#drawable/ok_button_selector" instead of the usual image file.
Thats it! You don't need any extra code in your java onClick method.

Related

Android: change button background/text/compoundDrawables with selector XML

I have already been using selector drawables to make my button change background according to the state.
However, I also want to change the text color and left compound drawable together with the background. But the default selector XML atrribute does not contain any "android:textColor" or "android:drawableLeft" to be assigned.
I know I can always achieve this with extend my own button class, but is there any clean way out?
I am not very sure about drawables but for changing textcolor depending upon button state, I use selectors as below,
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#color/color_light_green"></item>
<item android:color="#fff"></item>
//state you want
</selector>
and then apply it to textColor attribute in the xml as,
android:textColor="#drawable/selector_btn_text_color"
Eclipse doesn't auto suggest color attribute in selector but we can do it. :)

Change the Style of an Image View on Pressed State in Android

I have an ImageView in my application. When user Clicks the imageView I am supposed to show a border around the ImageView. This Border style I have it as a style called "myStyle" in my styles.xml.
I need to show this style only when the user clicks the image view. How can I do this?
Well, i will advice you to get the border style info into an xml file and save it at the drawable folder - let's call it border_pressed.xml.
And then at the drawable folder create a file called, let's say, imagview_state.xml and put in it the next code -
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/border_pressed" android:state_pressed="true"></item>
</selector>
and at the layout file - where the ImageView is- set it's background like that -
android:background="#drawable/imagview_state"

i have image button and i wont to change the image when i click on it

I have two images, image 1 and image 2, and I have image_button which has image 1 as a button background. Now what I want to do is, if I press on this image_button, I want the image background to change to image 2, and if I take my finger off the image_button, I want the image 1 to be back again like it used to be.
How can I make this?
You can achieve this programatically when a click occurs :
ImageView imageIcon = (ImageView) findViewById(R.id.icon);
imageIcon.setImageResource(R.drawable.other_icon);
You want something called a selector. In your drawable folder, create a xml file with these contents:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item android:drawable="#drawable/image1" android:state_pressed="true"/>
<!-- default -->
<item android:drawable="#drawable/image2"/>
</selector>
Selectors are really useful for this kind of stuff, you should read the documentation
Basically, give this thing a name, like yourSelector.xml
You can use this the same as any other drawable, like #drawable/yourSelector or R.id.yourSelector (make sure not to write '.xml' though)

Android: how add opacity on ImageButton click?

I thought to use OnTouchListener and tracking down and up events, setting then an alpha value, but I have to add this listener to all ImageButtons (they are really a lot). I wonder if there is a shortcut to achieve this result.
In case that when user click on button and this cause to change the opacity of that button you can do:
In your xml file on button declaration add this line:
android:onClick = "clickMethod"
and in the java file you need to implement the clickMethod,
public void clickMethod(View view)
{
// change opacity
}
so, if you want to do the same process(change button opacity) for each button, so in the xml file
for each button add the line
android:onClick="clickMethod"
If you want to give users better expirince when clicking ImageButton, I recommend you to use selection drawable as the background of your ImageButtons. It gives better user experience, then setting opacity while performing click and it’s really easy to achieve.
First you need to create in your drawable folder file with name f.e. image_button_selection.xml. In which you should define:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/your_focused_background_drawable" />
<item android:state_pressed="true" android:drawable="#drawable/your_pressed_background_drawable " />
<item android:drawable="#android:color/transparent" />
</selector>
You should also put into drawable two png with alpha channel that will be displayed on focused button and pressed button. In this example they should be named respectively your_focused_background_drawable and your_pressed_background_drawable.
When you do this you should in every use of your ImageButton in xml use following statement:
android:background="#drawable/image_button_selection"

custom android button look and feel

The two buttons in the bottom of the screen are Scan and Leads, if we click on any button, the view of the button is visible like pressed as u see in image for lead button, this view will change according to the button click, and here i am unable to create that kind of look and feel for buttons. please provide me the sample code or suggest me to achieve this look and feel.
Thanking you
this is custom button .A custom button is very easy to create, you just have to make different images for the different state of the button and place it into your res/drawable folder.
below link contain the step by step description for creating this:
http://androidemulator.wordpress.com/2011/11/03/creating-custom-buttons-in-android-applications/
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_some_name_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/button_some_name_default"/>
</selector>
Have this in your /drawable/ folder as button_some_name.xml
Then have the button_some_name_pressed.png & button_some_name_pressed.png in your drawable-ldpi drawable-mdpi etc
There are other states to consider as well
ref: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Categories

Resources