3

I am trying to get the following state list to work. The idea is to create a white background.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_selected="true"         
android:drawable="@android:color/transparent" /> 
<item android:state_selected="true" android:drawable="@android:color/transparent" /> 
<item android:state_pressed="true" android:state_selected="false"    
android:drawable="@android:color/transparent" /> 
<item android:state_selected="false" android:drawable="@color/WHITE" /> 
</selector>

When I try and compile this I get the following error..

@color/transparent and @color.WHITE don't exist.

Do I need to define these somewhere and if so how ?

Thanks !

Deepzz
  • 4,573
  • 1
  • 28
  • 52
GuybrushThreepwood
  • 5,598
  • 9
  • 55
  • 113

5 Answers5

1

In Android's Color Palette there is no defined transparent Android Color Palette, however you could define opacity and that will generate the sense of transparency, here is a good sample about how to use it: Hex transparency in colors

Here's a code snipet about how to declare a custom color in res/value/colors.xml

<color name="colorWhite">#FFFFFF</color>

now with transparency:

<color name="colorWhite">#FFFFFFFF</color>

being used in the state list:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/colorWhite" android:state_hovered="true"/>
</selector>
1

I think the only problem is you are referencing directly a color instead of an actual Drawable. Try creating a ColorDrawable first and use it in your StateListDrawable.

When creating a ColorStateList (which is different from StateListDrawable because it's a list of colors and not Drawables) you can directly use colors ...

Cyril Mottier
  • 1,614
  • 1
  • 12
  • 10
1

You must use @android:color, otherwise it doesn't know where the color is defined.

android:background="@android:color/white"
Charles Menguy
  • 40,830
  • 17
  • 95
  • 117
Sea turtle
  • 467
  • 1
  • 6
  • 17
0
<color name="white">#aarrggbb</color> will set the transparency
<color name="white">#80ffffff</color> will set white color with transparency value 80.
Vatine
  • 20,782
  • 4
  • 54
  • 70
Achilles
  • 1,065
  • 2
  • 13
  • 29
0

try to create a resource file like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffff</color>
</resources>

let's see if someone knows about transparent one

Deitools
  • 421
  • 6
  • 16