I have a png button, which is enabled, non-pressed. When user click the button, I want only to darken the png. I need something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
//normal button with background my_button.png
<item
android:state_enabled="true"
android:drawable="@drawable/my_button" //my_button.png
/>
//pressed button with background my_button.png overlayed by 50% black
<item
android:state_pressed="true"
android:state_enabled="true"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<bitmap android:src="@drawable/my_button"/>
<color android:color="#00000088"/>
</RelativeLayout>
</item>
</selector>
Is there some way how to do that? Or do I must have another png file?
Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure. Step 3: Then a pop-up screen will arise like below.
To use an image resource, add your file to the res/drawable/ directory of your project. Once in your project, you can reference the image resource from your code or your XML layout. Either way, it's referred to using a resource ID, which is the file name without the file type extension. For example, refer to my_image.
In my_button_bg.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/button_normal"/>
</selector>
button_normal is a png
button_pressed is an xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/button_normal"/>
<item android:drawable="@color/btn_bg_pressed_mask"/>
</layer-list>
where btn_bg_pressed_mask is a color:
<color name="btn_bg_pressed_mask">#19000000</color>
This should work
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:drawable="@drawable/my_button" />
<item>
<selector>
<item
android:state_pressed="true"
android:state_enabled="true">
<color android:color="#00000088" />
</item>
</selector>
</item>
</layer-list>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With