HelloCW
HelloCW

Reputation: 2255

What is the different between the folder "drawable" and the file "drawable.xml"?

In the activity_main.xml, there are two rows code android:background="@drawable/border_ui" and android:background="@drawable/my",

but the one use the border_ui.xml located the folder res\drawable, and the other use the file drawable.xml located the folder res\values.

What different? Thanks!

And more, an error will occur if I remove the file drawable.xml to the folder res\drawable and rename it as my.xml.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" 
    android:background="@drawable/border_ui"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:background="@drawable/my"
        />

</RelativeLayout>

border_ui.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >   
    <stroke android:width="1dp" android:color="#000000" />     
    <solid android:color="#ffffff" />
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

</shape>

drawable.xml

<resources>
  <drawable name="my">#f00</drawable>
</resources>

Upvotes: 3

Views: 1786

Answers (4)

ChiefTwoPencils
ChiefTwoPencils

Reputation: 13930

The difference is one, the border, is involving a shape which is a drawable entity. The other, the values resource, is referencing a color which is acceptable as a drawable as shown in a note here - like so =>

Note: A color resource can also be used as a drawable in XML. For example, when creating a state list drawable, you can reference a color resource for the android:drawable attribute (android:drawable="@color/green").

I would imagine the background in question looks something like this. (#f00 represents a websafe color)

Edit: Please see this article about color drawables.

What it says, and what the Android Drawable Resources shows, is that files used for specific purposes like any of the drawable types shown require certain tags to be the root. Just removing the <resource> tag as has been suggested is not going to work. If you're wanting to force the color drawable into another resource folder other than where it belongs (values), which I can't really see why, then you're going to have to hack it.

Final word:
You asked why they're in different places and why you can't just rename and move it into another folder of your choosing - both of which have been answered. I believe if you're expecting a solution on moving the file to the drawable folder then another question asking such would be in order. Please consider upvoting or accepting an informative answer.

Upvotes: 1

blay
blay

Reputation: 464

it's from android documention about String but it's can explain about :

Note: A string is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). So, you can combine string resources with other simple resources in the one XML file, under one element.

the syntax of drawable is without element but regular xml file

Upvotes: 1

Kumar Bibek
Kumar Bibek

Reputation: 9117

You can change the name of the file, but don't move it to the drawable folder. Keep it where it is currently, and it should work fine. drawable.xml is not a keyword. But you can't have this file, which contains a list of resources in the drawable folder.

Since your drawable is a resource, it should be inside the values folder instead of the drawable folder.

Upvotes: 1

R&#233;mi F
R&#233;mi F

Reputation: 1335

In fact, the drawable.xml file is a file containing resources, but not only drawables.

So, if you want to move it to the folder drawable and rename it my.xml, it is ok. You should just remove the <resources> & </resources> tags and it should work.

Upvotes: 0

Related Questions