Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set bitmap in layer list Android

I need programmatically set different icons in layer list by @+id/calendar_day_bottom_icon

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

    <item android:drawable="@drawable/calendar_date_today_bg"/>

    <item android:id="@+id/calendar_day_bottom_icon" >
        <bitmap
            android:src="@drawable/ic_calendar_journal"
            android:gravity="bottom|center" />
    </item>
</layer-list>

In code I do next thing

LayerDrawable layerDrawable = (LayerDrawable) cellView.getBackground();          
BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.ic_calendar_journal_reminder);
layerDrawable.setDrawableByLayerId(R.id.calendar_day_bottom_icon, drawable);

But icon inserts incorrectly.

I want this one: enter image description here

But there is: enter image description here

like image 302
elya_a Avatar asked Oct 15 '25 05:10

elya_a


1 Answers

I find the solution with another drawable file:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <bitmap
            android:gravity="bottom"
            android:src="@drawable/ic_calendar_journal" />
    </item>
</layer-list>

get drawable

LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(context, R.drawable.calendar_layers);

get LayerDrawable, get layer item and set it to another LayerDrawable.

LayerDrawable cellLayerDrawable = (LayerDrawable) cellView.getBackground();
Drawable drawable = layerDrawable.getDrawable(0);                    
cellLayerDrawable.setDrawableByLayerId(R.id.calendar_day_bottom_icon, drawable);
like image 131
elya_a Avatar answered Oct 17 '25 20:10

elya_a



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!