Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<item> tag requires a 'drawable' attribute

I am trying to very simply style a Button. I just want it blue with with text when not pressed, and white with blue text when clicked.

I tried to do this with a style and a selector.

In my layout I have this Button:

 <Button
    android:id="@+id/button1"
    style="@style/MyButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="@string/login" />

And in res/values/styles I have this styles.xml:

<style name="MyButton">
    <item name="android:background">@drawable/btn_background</item>
    <item name="android:textColor">@drawable/btn_textcolor</item>
</style>

And of course, the two selectors, in res/drawable, btn_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:state_pressed="true" android:color="@color/white" />
   <item android:color="@color/SapphireBlue" />
</selector>

and btn_textcolor.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:color="@color/SapphireBlue" />
    <item android:color="@color/white" />
</selector>

The error I get now when I either run the app, or open the layout editor is:

<item> tag requires a 'drawable' attribute

I understand the message, but I don't have a drawable, is it is a simple, flat button.

How can I create such a simple button?

Update according to this post, it should work.

like image 666
Bart Friederichs Avatar asked Sep 20 '13 09:09

Bart Friederichs


People also ask

What is a drawable?

A drawable resource is a general concept for a graphic that can be drawn to the screen and which you can retrieve with APIs such as getDrawable(int) or apply to another XML resource with attributes such as android:drawable and android:icon . There are several different types of drawables: Bitmap File.

How do I add an image to a drawable XML file?

Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure.


1 Answers

Try this way,hope this will help you to solve your problem.

btn_background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/white" android:state_pressed="true"></item>
    <item android:drawable="@drawable/white" android:state_focused="true"></item>
    <item android:drawable="@drawable/SapphireBlue" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
    <item android:drawable="@drawable/white" android:state_enabled="false"></item>
</selector>

btn_textcolor.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/SapphireBlue" android:state_pressed="true"></item>
<item android:color="@drawable/SapphireBlue" android:state_focused="true"></item>
<item android:color="@drawable/white" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:color="@drawable/SapphireBlue" android:state_enabled="false"></item>
</selector>
like image 60
Haresh Chhelana Avatar answered Sep 28 '22 10:09

Haresh Chhelana