Search code examples
androidxmlbuttondrawable

Android Studio drawable selector unknown issue


I seem to have a problem that I just do not know how to solve with Android Studio. The code worked find on Eclipse and the weird thing is that it also works on the Design preview window of Android Studio, but does not work when I execute the application on any real device. The problem I'm having is with the drawable folder where I have a selector xml and a shape xml file. In my layout, I set the background of the button to the selector xml file. I noticed a very annoying red circle with an exclamation mark in the selector xml file where I specify the drawable to be the shape xml file. It doesn't state what the problem is, if any. It looks just like the following picture:

enter image description here


The Design preview window shows the button as specified in the drawable xml files: enter image description here

Here is the code for my files:

bshape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <solid android:color="#FFAA33"/>
    <corners android:radius="5dp" />
</shape>

button.xml (selector)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Normal state of button -->
    <item
        android:state_enabled="true"
        android:drawable="@drawable/bshape" />

</selector>

myLayout (RelativeLayout)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:visibility="visible" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="Button Test"
            android:textColor="#000000"
            android:background="@drawable/button"/>

</RelativeLayout>

I've made sure the xml files have the .xml extension. I've noticed several posts regarding missing the extension in the xml file. This is really driving me nuts. I've spent about 3 hours attempting to solve this problem and I just don't know what it could be. Any help is extremely appreciated. Thank you in advanced!

Edit: Below is a picture of all the files listed in my project. enter image description here


Solution

  • First, I want to thank Eric for his attempt at providing a solution to my problem. Thank you very much Eric!

    It seems to be working, however the red circle with the exclamation point still shows in the button.xml selector file.

    This is what I did, I used the Design view to select the button and under the Properties pane, I clicked on background ... button and the Resources window came up. I selected the Project tab, then I searched for Drawables->button and clicked OK.

    I performed a 'Clean Project' by going to Build->Clean Project. Then I build the project by executing Build->Rebuild Project in the Menu bar.

    Now it seems to be working as expected. I believe this could very well be a bug in Android Studio. There goes 4hrs of my life I will never get back :)