4

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

3
  • use bottom instead of bottom|center. Commented Aug 2, 2016 at 9:40
  • android:gravity="bottom|center" to android:gravity="bottom" Commented Aug 2, 2016 at 9:46
  • it doesn't work :((( any ideas? Commented Aug 2, 2016 at 9:48

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.