/CustomImageButtonBackgroundAndSourceChangeOnPress

Ever felt like you need to change both the background and the Image source (srcCompat or src) at the same time, this will help.

Primary LanguageJava

Custom ImageButton - Background And Source Change On Button Press

If you need to change both the background and the Image source (srcCompat or src) at the same time during button press, this will help.

Credits:

Normal State vs Button Pressed State

Normal View Pressed State

Steps to try:

  1. Add the maven url to your Project Level Gradle File build.gradle(Project:).
allprojects {
 repositories {
	...
	maven { url 'https://jitpack.io' }
 }
}

1.1. Add the implementation to your App Level Gradle File build.gradle(app:) :

implementation 'com.github.ReejeshPK:CustomImageButtonBackgroundAndSourceChangeOnPress:1.0'
  1. Use the ImageButtonView
<com.blogspot.cavemanbacktocave.buttonpressbgsrc.MyImageButton
        android:layout_width="100dp"
        android:layout_height="100dp"        
        app:normalBackground="@drawable/my_background_normal_state"
        app:normalSrc="@drawable/ic_arrow_forward_normal"
        app:pressedBackground="@drawable/my_background_pressed_state"
        app:pressedSrc="@drawable/ic_arrow_forward_pressed" />

Left Screen Shot (Normal State):

app:normalSrc is the image source which is normally visible. In our screen shot, it is the right arrow > .

app:normalBackground is the background which is normally visible for that image button. In our screen shot, it is the round background

Right Screen Shot (Pressed State):

app:pressedBackground is the Background which you want to show when the button is pressed. In our screen shot, it is the green background.

app:pressedSrc is the image or icon which you want to show when the button is pressed.