Search code examples
androidxmlandroid-layoutandroid-drawable

how to have an inline drawable inside layout xml


I have some drawable resources that are used only in a single xml layout. I would like to insert them into the specific xml layout (inline) so i could have a better organization of my files and a less overloaded drawable folder

i found this topic: https://developer.android.com/guide/topics/resources/complex-xml-resources

but this using an inline drawable as resource for another one... i would like to use it in a layout xml

EXAMPLE:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/round_corner"
    android:orientation="horizontal"

    android:padding="@dimen/smallMargin">

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/buttonDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:drawableTop="@drawable/code"
        android:text="@string/delete"
 />
</androidx.appcompat.widget.LinearLayoutCompat>

where code.xml is:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:width="@dimen/popupButtonSize"
        android:height="@dimen/popupButtonSize"
        android:drawable="@drawable/ic_population" />

</layer-list>

code is only used in that specific layout, is there any way to make it inline?


Solution

  • What seems to be the issue? If you follow the guide it works without a problem:

    1. Add xmlns:aapt="http://schemas.android.com/aapt" to root node
    2. Replace android:drawableTop="@drawable/code" attribute with aapt:attr child

    Final XML will look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.appcompat.widget.LinearLayoutCompat
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:aapt="http://schemas.android.com/aapt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/round_corner"
        android:orientation="horizontal"
        android:padding="@dimen/smallMargin">
    
        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/buttonDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="@string/delete">
    
            <aapt:attr name="android:drawableTop">
                <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    
                    <item
                        android:width="@dimen/popupButtonSize"
                        android:height="@dimen/popupButtonSize"
                        android:drawable="@drawable/ic_population" />
    
                </layer-list>
            </aapt:attr>
    
        </androidx.appcompat.widget.AppCompatButton>
    </androidx.appcompat.widget.LinearLayoutCompat>