Is there a way to set drawable's Alpha using XML?

AndroidDrawableAlpha Transparency

Android Problem Overview


Easy like itself . I wanna make an alpha button , which would have a selected drawable this way:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <!-- Play/Pause -->
    <item android:state_selected="false" android:drawable="@drawable/item" />
    <item android:state_selected="true" android:drawable="@drawable/item" />
	
</selector>

I would wanna make something like this:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <!-- Play/Pause -->
    <item android:alpha="125" android:state_selected="false" android:drawable="@drawable/item" />
    <item android:alpha="255" android:state_selected="true" android:drawable="@drawable/item" />
	
</selector>

Thanks for all .

Android Solutions


Solution 1 - Android

It's been a while since the OP, but personally found a solution that worked a lot better for me than the suggested answers. Creating a BitmapDrawable makes is easily possible to set the alpha:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/your_drawble"
    android:alpha="77">
</bitmap>

Alpha can be any value between 0 and 255. Note that it is sort of the inverse of the HEX color value alpha, as for example 70% alpha would be B3 in HEX and 77 in the BitmapDrawable.

Solution 2 - Android

I achieved the same using a drawable

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#5000ddff" />
</shape>

Over here used the alpha 50, which sets the opacity level. Hope that helps

Solution 3 - Android

I have been looking for the same thing. Even though this is posted over four years ago, this is the top post when googling the issue, so I'll reply here.

This is my solution

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <bitmap android:alpha="@integer/not_pressed_alpha" android:src="@drawable/item"/>
    </item>
    <item android:state_pressed="true" android:drawable="@drawable/item" />
</selector>

Solution 4 - Android

For those who have the same problem as OP, AppCompat now allows you to set 'alpha' parameter, as he wished in his target code:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Play/Pause -->
    <item android:alpha="125" android:state_selected="false" android:drawable="@drawable/item" />
    <item android:alpha="255" android:state_selected="true" android:drawable="@drawable/item" />

</selector>

More info here.

Solution 5 - Android

My goal was to make a button have it's selected and pressed states at a different alpha - but using the same (png) resource and affecting as few files as possible.

My solution is similar to altering the alpha in a BitmapDrawable - but it does it from the selector so only one file is affected.

Use the tint function of Bitmap, remember that the tint will color the existing pixels so use a white color. Eg, #80FFFFFF - to keep color as original but reduce alpha by 50% This could also be used to change color of the icon when pressed.

This is my drawable XML file:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true">
        <bitmap
            android:src="@drawable/ic_camera"
            android:tint="#80FFFFFF">
        </bitmap>
    </item>
    <item android:state_pressed="true">
        <bitmap
            android:src="@drawable/ic_camera"
            android:tint="#80FFFFFF">
        </bitmap>
    </item>
    <item>
        <bitmap
            android:src="@drawable/ic_camera">
        </bitmap>
    </item>
</selector>

Solution 6 - Android

I'm using the following for a custom radio button which should be diagonally strikethrough when it is disabled.

Example Image of 5 radio buttons where 4 of them are enabled

  <item android:state_enabled="false">
    <layer-list>
      <item>
        <shape android:shape="rectangle">
          <size
              android:height="35dp"
              android:width="35dp"/>
          <stroke
              android:color="@color/someGrey"
              android:width="1dp"/>
          <corners android:radius="1dp"/>
        </shape>
      </item>
      <item>
        <rotate
            android:fromDegrees="135"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="135">
          <shape android:shape="line">
            <stroke
                android:color="@color/someGrey"
                android:width="1dp"/>
          </shape>
        </rotate>
      </item>
    </layer-list>
  </item>

Solution 7 - Android

I agree with Kasium sugest, so for some Android versions the especification to Alpha is in percent.

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/your_drawble"
    android:alpha="0.5">
</bitmap>

Solution 8 - Android

i think you could create your own drawable which could take this argument as a parameter. i've never done such a thing though.

check out this link :

https://stackoverflow.com/questions/6705304/how-to-set-alpha-value-for-drawable-in-a-statelistdrawable

if that's not possible, you can always do it in code...

here are 2 links i've found about it, in case you wish to use bitmaps instead:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionA.QuirogaView Question on Stackoverflow
Solution 1 - AndroidKasiumView Answer on Stackoverflow
Solution 2 - AndroidPrashantView Answer on Stackoverflow
Solution 3 - AndroidFredrik MetcalfView Answer on Stackoverflow
Solution 4 - AndroidkikettasView Answer on Stackoverflow
Solution 5 - AndroidRichard SahlinView Answer on Stackoverflow
Solution 6 - AndroiddnldttmrView Answer on Stackoverflow
Solution 7 - AndroidFernando SilvaView Answer on Stackoverflow
Solution 8 - Androidandroid developerView Answer on Stackoverflow