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:
-
I built this from references from Stack Overflow from these developers and links:
-
Defining Custom Attributes:
-
https://stackoverflow.com/questions/3441396/defining-custom-attrs
-
For Publishing Reference :
-
How to create your own Android Library and publish it - Anuj Gupta
-
https://medium.com/@anujguptawork/how-to-create-your-own-android-library-and-publish-it-750e0f7481bf
Normal State vs Button Pressed State
Steps to try:
- 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'
- 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.