Setting alpha of drawable through xml
-
10-02-2021 - |
Question
Can we set the alpha of drawable for its disable state through XML? For example by using android:state_disable.
Solution
actually we cant set alpha for drawable instead you should use this setAlpha(int alpha) Specify an alpha value for the drawable.
OTHER TIPS
This is my solution, hope someone finds this useful. Yes, this is and old post, but posts like this need a answer that works today since they show up when you search.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<bitmap android:alpha="@integer/disabled_alpha" android:src="@drawable/item"/>
<item/>
<item android:state_enabled="true" android:drawable="@drawable/item" />
</selector>
Drawable card_water_wave = getResources().getDrawable(R.drawable.textview_back);
card_water_wave.setAlpha(150);
See my answer to a similar question: https://stackoverflow.com/a/29078730/562365
Basically you can use the alpha of a BitmapDrawable and then use that drawable in your selector.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow