Change the Style of an Image View on Pressed State in Android - 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"

Related

how to keep default click effect when I change button background in Android?

when I use default background on button, I click button, the button's background will be dark gray.
But when I change button background (custom image) , the default click effect(dark gray) is disappeared.
how to keep default click effect when I change button background in android?
Use this attribute
android:foreground="?attr/selectableItemBackgroundBorderless"
Well then you can call it a Selector and not Click Effect. What happens is that you make a file in drawable folder with alternative background colors or images to different states of the Button. A selector file can be like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/image_when_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/image_when_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/image_normal"></item>
</selector>
So you can put images or colors but at the end you will have to set the whole file as your background to a Button like:
<Button
android:id="#+id/button"
android:background="#drawable/my_selector"
android:layout_width="200dp"
android:layout_height="126dp"/>
So the default button clicks had a default selector, if you change the background also change the selector using images or colours.

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. :)

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)

ImageButton Android round Image without square border

When I create a round ImageButton, there is also a transparent square border around the object. It shows wenn I click on the ImageButton in the Graphical Layout of the xml file.
How can I remove this border?
You can set custom background for the button,
You can specify two images(rounded) for the two states(pressed and not pressed), so that the only two round shaped images wold cover your image, default rectangular would go. I hope It helps you.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/skyblueBackground" android:state_pressed="true" />
<item android:drawable="#color/transparentBackground" />
</selector>
I tried with colors, you can use images. You just want to save it in your drowable folder and specify it for the background of button

How to use a selector to change an ImageButton image?

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.

Categories

Resources