How to change drawable image background while click - android

I am new to Android, I have an image, want to change background color programmatically while clicking that icon. How to do. Please, anyone help me.
I have icon like this, before click:
I want to change like this while click, after click:

You have to see:
how to apply click event listener to image in android
and inside of the click event listener:
backgroundImg.setBackgroundColor(Color.parseColor("#FFFFFF"));
or
backgroundImg.setBackgroundColor(Color.rgb(100, 100, 50));

What you need here is a selector.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
</selector>
And then you need to create the two images, because from what I see in the examples you also set the text color.
If you later want to change the overall color of the icon you can do that using AppCompatImageView and tinting.

Two Solution Available
Use Two Images and update on android:state_selected, android:state_pressed. Similar to dzsonni answer.
Use SVG Image.Update Programmatically.See Below link
Dynamically changing SVG Image colours on Android

Related

Why does a Button loses its original behaviour after changing its color?

I am writing an Android app and now I am styling it. I am using a custom theme which is a child of Theme.Holo.Light. I really like Theme.Holo.Light because its buttons have a special effect when you click and hold it. Like the lower button in the picture below:
Not click:
click:
The upper button has been changed color. Now when I click on that button, it doesn't have that effect. I really don't understand why. Can anyone tell me why this happens and how can I get the same effect with a colored button?
And also, the colored button seems fatter.
This is because the button uses a selector to display different colors/effects/drawables based on the state of the click. You can check out the link on Color State List Resource.
To create your own you have to create a slecetor cml file and put it in your drawables folder.
For example.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_btn_default_normal_gray" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#drawable/shape_btn_default_pressed_gray" android:state_pressed="true"/>
<item android:drawable="#drawable/shape_btn_default_disabled_gray"/>
</selector>
or with colors
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/dark_green" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#color/light_green" android:state_pressed="true"/>
<item android:drawable="#color/gray"/>
</selector>
To apply this you have to set the background drawable in your layout xml like this.
<Button
android:id="#+id/my_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some text"
android:background="#drawable/selector_btn_default_gray"/>
That is the "ripple effect" of material design.You have define you own style for that effect.Link below may help you or you may search for many other answers on StackOverflow. Material effect on button with background color
It does not loses its behavior you can see after click (in your second image) the button show same scale as the above have...so by default the background is set as to show that it is button (like with padding or so) and can changes to show oncklick effect...
So when you set your desire background to button...It takes complete change on what is on presently on it and then you have to manually set onclick effect..

How to add a click effect to a button on click event?

How would I add a click effect similar to the below image for a button click event?
The button in my activity already has it's background set to an image so I'm not sure how I would add a background of a state also as in this tutorial:
Android Button color changing on onClick?
You need two image, one for normal state and another for pressed state. At first create a selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/pressed_image" android:state_pressed="true"/>
<item android:drawable="#drawable/normal_image"/>
</selector>
Add this selector in your button as a background.
Here's something similar. I am not sure though, if this fits your solution.
EDIT: It uses selector, just like #Raghunandan suggested above.

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 textColor selector

I am trying to set up a selector for TextView textColor using the following code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="?android:attr/textColorTertiary" />
<item android:color="?android:attr/textColorPrimary"/>
</selector>
However, the color always appears to be red instead of those theme colors. If I put hardcoded color, everything seems to work fine. (ex. <item android:state_enabled="false" android:color="#666666" /> ).
What is the problem and how to solve it? P.S. if anyone knows how to set theme's default disabled color for disabled item in the list, please share, that is what I am trying to achieve. Thanks.
As far as i can see you may have to use 3 states in a selector.
state enabled
state focused
state pressed
in exactly this order. This might help
You used selector for what reason?
If you want to make your text of text view always red then no need of selector. Just define color in color.XML or in string.XML using add color.
And if you want to chanhe it on selection or focus than use the states.
state enabled
state pressed
state focused
Than it will work as you need.

Android - How to create image button of different states, default , pressed and focussed

In my android application i want to create three different image of a single image for each state pressed , default and focussed. Is there any tool that can help me to create these images.
I think you misunderstood my problem, i knows that how to use three different image for three different states. I only wants to know that how two create those three images.
here http://code.google.com/p/iosched/source/browse/android/#android%2Fres%2Fdrawable-hdpi
you can see that there are three different images for three differenct states
home_btn_announcements_default.png
home_btn_announcements_pressed.png
home_btn_announcements_selected.png
I wants to know that is there any tool that can create the above three images. I think now my question is clear to everyone.
My Question was that how to add white border for default image , gray border for focussed image. I know that how to change image according to different states. I want to ask you that is there any tool that can put white or gray border around my image.
Use selector save this file in drawable/click.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:drawable="#drawable/notPressedImage ></item>
<item android:state_pressed="true" android:drawable="#drawable/PressedImage" ></item>
</selector>
use click.xml (click) as button src...

Categories

Resources