I have this code in an xml inside the drawable
folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<layer-list>
<item>
<shape android:shape="rectangle">
<size android:width="90dp" android:height="90dp" />
<solid android:color="#9933CC" />
</shape>
</item>
<item>
<bitmap android:gravity="center" android:src="@drawable/main_achievements_synthesis" />
</item>
</layer-list>
</item>
<item>
<layer-list>
<item>
<shape android:shape="rectangle">
<size android:width="90dp" android:height="90dp" />
<solid android:color="#AA66CC" />
</shape>
</item>
<item>
<bitmap android:gravity="center" android:src="@drawable/main_achievements_synthesis" />
</item>
</layer-list>
</item>
</selector>
I use it to have an image with 2 states (to use as a button). Everything works as expected on emulator and devices.
I know that I can create different drawable xml and make a reference to achieve the same result.
I just switched to Android Studio and it shows me this mesage: Element XXX is not allowed here
. It warns me about Layer-list and all the tags inside it. However, as I said, this code works just fine.
Should I switch my code into separated XMLs (knowing that I will only use them once) or is an "error" in the Android Studio's Inspector Code?
NOTE: I think my code can be optimized, but I haven't figured out how yet.
This is an error in Android Studio's linting tool. Please file a bug at https://code.google.com/p/android/issues/list.
Also, you can optimize a little by using:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<selector>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<size android:width="90dp" android:height="90dp" />
<solid android:color="#9933CC" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<size android:width="90dp" android:height="90dp" />
<solid android:color="#AA66CC" />
</shape>
</item>
</selector>
</item>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/main_achievements_synthesis" />
</item>
</layer-list>