GuybrushThreepwood
GuybrushThreepwood

Reputation: 5616

Creating State List Drawable XML for Background - Android

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 !

Upvotes: 3

Views: 4019

Answers (5)

Daniel Lobito Olvera
Daniel Lobito Olvera

Reputation: 53

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>

Upvotes: 1

Achilles
Achilles

Reputation: 1065

<color name="white">#aarrggbb</color> will set the transparency
<color name="white">#80ffffff</color> will set white color with transparency value 80.

Upvotes: 0

Sea turtle
Sea turtle

Reputation: 467

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

android:background="@android:color/white"

Upvotes: 1

Cyril Mottier
Cyril Mottier

Reputation: 1624

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 ...

Upvotes: 1

Deitools
Deitools

Reputation: 431

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

Upvotes: 0

Related Questions