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

9

56

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 .

Norword answered 18/11, 2011 at 7:45 Comment(2)
I am still trying to figure this out as well. Any luck?Matelot
Replace your XxxView by AppCompatXxxView in your layout XML and follow @kikettas´ advice: https://mcmap.net/q/1470902/-is-there-a-way-to-set-drawable-39-s-alpha-using-xmlInge
P
57

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.

Portis answered 16/3, 2015 at 14:2 Comment(6)
Actual, alpha in xml was float type, it should be value between 0.0 and 1.0.Remsen
@Remsen That might be true for views, but not for <bitmap>. The ability to set the "alpha" of a <bitmap> is a currently undocumented feature which might or might not work. The value also does not really represent the alpha value in the traditional sense but rather its oposite, the opacity, that is the reason why - if it happens to work - you would get a completely transparent bitmap for the value of 255 and and full opacity with the value of 0.Graniela
@Graniela so is there a way to do this in XML, or are we out of luck?Matelot
@Remsen as far as the Android documentation goes regarding this, i suppose that we are out of luck. the undocumented alpha attribute seems to work on Android 5+ but that won't help if one wants to support earlier versions. You could generate transparent resources or attempt to use icon fonts. wrapping the bitmap in a NinePatch seems to also work on Android 5+, but I haven't tested it on earlier versions. please tell, if you find another option.Graniela
Just for reference, in Android Studio 2.0 Preview 2, the ide is explicitly asking me to set a float value (so between 0.0 and 1.0 as @Remsen explained). Everything else is just flagged as an error.Hicks
This only works for actual images, not any Drawable (e.g. color).Cyprinid
M
52

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.

Mandal answered 24/8, 2013 at 20:29 Comment(6)
This is useful. Using this in combination with a layer-list and your own drawable item can create a drawable for a pressed state.Baumgardner
i didn't get it, how is it gona change the alpha of a drawable?Dulcet
Hex values are described in pairs. The first 2 represent the alpha (for Android anyway, sometimes it's the last 2). The next 3 pairs represent Red - Blue - Green #5000ddff alpha(50) red(00) blue(dd) green(ff)Candlepin
Unfortunately this is not answering the OP, which was about changing the alpha of a drawable, not a color.Ambrosane
answer is correct, but 50% alpha in hex is 256/2 = 128 and therefore #80Dunkle
sometimes this doesnot work properly on android API 21 devices. checked on a similar emulator. whats the fix for thatSour
H
20

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>
Hashum answered 22/4, 2016 at 12:58 Comment(3)
I tried this. Not working for me. Tested on emulator(Nexus 5X API 23).Obcordate
It worked after changing state_selected to state_pressed.Obcordate
Yes, that would be the normal case, but I used the example code from the question.Hashum
B
16

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.

Ballata answered 21/6, 2016 at 14:42 Comment(3)
"Starting with API 23, items may optionally define an android:alpha attribute"Fluorescence
Isn't this just for colors though and not for drawables?Revegetate
shouldn't the alpha value here be in range from 0 to 1 ?Contradict
S
5

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>
Savagery answered 18/1, 2017 at 8:30 Comment(0)
M
2

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>
Modena answered 30/6, 2019 at 10:41 Comment(1)
This is exactly what I was looking for! Gracias por la respuesta senor Silva!Acuff
S
1

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>
Silverman answered 16/5, 2019 at 9:53 Comment(0)
N
1

Its not that simple but its possible:

First you have to define color in color folder of your resources like this:

color/inst_control_pressed_transp.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:alpha="0.5" android:color="?attr/inst_control_pressed" />
</selector>

Now you can reference that color from some shape:

drawable/background_clear_pressed.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid
        android:color="@color/inst_control_pressed_transp" />
</shape>

Tha you can use it in drawable:

drawable/background_bordered_clear_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/background_clear_pressed" android:state_pressed="true" android:state_selected="false" />
    <item android:drawable="@drawable/background_clear_active" android:state_activated="true" />
    <item android:drawable="@drawable/background_clear_selected" android:state_selected="true" />
    <item android:drawable="@drawable/background_bordered_clear_round" />
</selector>
Nickolasnickolaus answered 9/8, 2022 at 20:56 Comment(0)
S
0

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 :

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:

Scanty answered 24/8, 2013 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.