'How to make button Change image src when hover on it on android studio

Hey there i want to make a button change his own background image when i just touch it and in the second i let go it will change back to the original image

like adding a frame when the button is touched to indicate the button will do something when letting him go, Now i already manage to design the two images of the button states but i just cant find out what the operation of hovering over the button called

state1

enter image description here

state2

enter image description here



Solution 1:[1]

You need a Selector to do this.

<Button
     android:id="@+id/myButton"
     android:background="@drawable/selector_my_button"
     android:layout_width="100dp"
     android:layout_height="36dp"
     android:text="My Button" />

And, in the selector_my_button.xml file, you can described the different states you want to customize

<?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/button_state_pressed"/> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@drawable/button_state_focus"/> <!-- focused -->
    <item android:drawable="@drawable/button_state_default"/> <!-- default -->
</selector>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bruno