How to make a button with a custom background image show click animation on Android

How to make a show button, it is pressed (by changing it down / changing) for buttons using a custom background image on Android. I don’t want to include more images and set different ones for different states, as shown in the google hello views example. Thanks.

+7
android
source share
4 answers

It is possible to use only one image file using the ColorFilter method. However, ColorFilter expects to work with ImageViews, not buttons, so you need to convert your buttons to ImageViews. This is not a problem if you use images as buttons in any case, but it is more annoying if you have text ... In any case, assuming you find a way to solve the problem with the text, the code is used here:

ImageView button = (ImageView) findViewById(R.id.button); button.setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY); 

This is a red overlay on the button (the color code is the hex code for a fully opaque red color - the first two digits are transparency, then RR GG BB.).

You can make your ImageViews look like regular buttons by copying the btn_default_normal.9.png file from your sdkfolder / platform / (version and data of Android / res / drawable to your own project. Then in your ImageView use android:background="@drawable/btn_normal_default" and android:src="..." to set the image inside the button.

+2
source share

for this you need to use two images.

  • button_normal
  • button_pressed

then create an xml resource in a migrationable folder

 <?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/button_normal" /> <item android:state_pressed="true" android:drawable="@drawable/button_pressed" /> </selector> 

then set this file as the background for the image. here we use imageview as a button. do not forget to include these two buttons in the drop-down folder. It is he.

+12
source share

You can create a simple transparent black image and create a list of levels that you can use so that you can put a transparent image on top of the actual image. This gives a repulsive effect. You can use this as an onclick image as shown below.

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/image"> </item> <item android:drawable="@drawable/transparent_image"> </item> </layer-list> </item> <item android:drawable="@drawable/image"></item> </selector> 
+1
source share

just add another easy way to do this: If your ImageButton stays in the background and you don't set it to null, it will work like a regular button and will show click animation, clicking exactly like other buttons. A way to hide the background while it is still there:

 <ImageButton android:id="@+id/imageButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="1dp" android:paddingLeft="1dp" android:paddingRight="1dp" android:paddingTop="1dp" android:src="@drawable/squareicon" /> 

the pads will not allow the background to be visible and make the button act like other buttons.

0
source share

All Articles