7

I'd like to use a selector to grey out an icon when it is not enabled. It seems that something like this should work (with a white background):

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" >
      android:alpha="0.5"
    </item>

    <item android:state_enabled="true" >
      android:alpha="1"
    </item>
</selector>

But it results in a java.lang.NumberFormatException during run time. I also tried "0.5f". Same error.

This is similar to Is there a way to set drawable's Alpha using XML? but I'm specifically asking about the NumberFormatException. Incidentally, I also tried using integer values between 0 and 255. I get the same error.

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101
  • 2
    Possible duplicate of [Is there a way to set drawable's Alpha using XML?](https://stackoverflow.com/questions/8179250/is-there-a-way-to-set-drawables-alpha-using-xml) – Nouman Ch Mar 18 '18 at 03:54

3 Answers3

4

Considering this is your color selector, which is located under res/color/my_selector.xml

Note: This works in device Android 23 and above

You can use alpha inside this color selector as

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/my_color" android:state_enabled="true" />
  <item android:alpha="@dimen/my_float_value" android:color="@color/my_color" />
</selector>

To use this -

<Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/button_label"
      android:background="@color/my_selector" />

Refer following link for more reference: https://developer.android.com/reference/android/content/res/ColorStateList.html

Jimit Patel
  • 4,265
  • 2
  • 34
  • 58
0

You can't do android:alpha that way inside an item. You could put something like a shape inside an item, or you can use drawable within item to achieve the same effect.

What do you mean by "grey out" though? What you have shown isn't really greying out so much as making something partially opaque. If the item was originally red and you change alpha to 0.5, it's still going to be a shade of red. But assuming we are starting at a color grey, in this case F5F5F5 (Material Grey 100), then you could do it this way:

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

    <item android:state_enabled="true"  android:drawable="#FFF5F5F5" />
    <item android:state_enabled="false" android:drawable="#80F5F5F5" />

</selector>

Notice the "80" in the false state giving a different opacity. Here is an example of doing something similar but with rectangular shapes inside the items:

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

    <item android:state_enabled="true">
        <shape android:shape="rectangle">
            <solid android:color="#FFF5F5F5" />
        </shape>
    </item>
    <item android:state_enabled="false">
        <shape android:shape="rectangle">
            <solid android:color="#80F5F5F5" />
        </shape>
    </item>
</selector>
Mark
  • 139
  • 1
  • 8
  • Thank you. I am aware of this method. I was hoping to find a solution which does not involve having two icons, or drawables. And, adjusting alpha gives a very good impression of "greying out", depending on what background color that bleeds through. – Peri Hartman Mar 18 '18 at 14:18
0

The alpha parameter must be in the item tag

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:alpha="1.00" android:color="@color/myColor" android:state_enabled="true"/>
    <item android:alpha="0.50" android:color="@color/myColor" android:state_enabled="false"/>
</selector>
J-Jamet
  • 847
  • 8
  • 17