androidbitmaplayer-list

How to Change Drawable item in Layer List Android Studio


Here it's my problem:

If I call the layer-list directly, I have no problem. But I want to change the drawable in the layer-list and when I do this on the java side, the drawable image I added takes up the screen.

Here is my flag xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- First assign id to the list item-->
<item android:id="@+id/color">
    <shape android:shape="rectangle">
        <corners android:radius="10dp"/>
        <stroke android:width="2dp"
            android:color="#000000" />
        <solid android:color="@color/colorAccent" />
    </shape>
</item>
<item android:id="@+id/change_flag" android:bottom="2dp">
    <bitmap android:src="@drawable/tr1" android:gravity="center"/>
</item>

here is java code

LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(this, R.drawable.flag);
        assert layerDrawable != null;
        Drawable drawable = ContextCompat.getDrawable(
                this, R.drawable.tr1);
        layerDrawable.setDrawableByLayerId(R.id.change_flag, drawable);

        buttons[i] = inflatedView.findViewById(getResources().getIdentifier("button" + i, "id", getPackageName()));
        
        buttons[i].setBackground(layerDrawable);

I want this
Text
But this happens
Text
I'm stuck i need some help.


Solution

  • It just a simple mistake just remove bitmap and give a drawable to item ...

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- First assign id to the list item-->
    <item android:id="@+id/color">
        <shape android:shape="rectangle">
            <corners android:radius="10dp"/>
            <stroke android:width="2dp"
                android:color="#000000" />
            <solid android:color="@color/colorAccent" />
        </shape>
    </item>
    <item android:id="@+id/change_flag" android:bottom="2dp" android:drawable="@drawable/tr1" android:gravity="center">
    </item>