SoFunction
Updated on 2025-03-11

Android has the effect of "press" when clicking ImageButton

Android has the effect of "press" when clicking ImageButton

1 After adding an image to ImageButton, there is a border, which looks like the image is pasted on a button, it is as ugly as it should be.

Solution: ImageButton background is set to transparent: #0000

2. In order to give users the "press" effect when using Button, there are two ways to implement it:

A.

(new OnTouchListener(){   
            @Override  
            public boolean onTouch(View v, MotionEvent event) {   
                if(() == MotionEvent.ACTION_DOWN){   
                    //Change to background image when pressed                    ();   
                }else if(() == MotionEvent.ACTION_UP){   
                    //Replace the picture when lifted                    ();   
                }   
                return false;   
            }   
        });  

B.

<?xml version="1.0" encoding="UTF-8"?>  
<selector xmlns:andro>  
  <item android:state_pressed="false" android:drawable="@drawable/button_add" />  
  <item android:state_pressed="true"  android:drawable="@drawable/button_add_pressed" />  
  <item android:state_focused="true"  android:drawable="@drawable/button_add_pressed" />  
  <item android:drawable="@drawable/button_add" />  
</selector>  

Thank you for reading, I hope it can help you. Thank you for your support for this site!