question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to set different drawables in same selector

See original GitHub issue

I need a custom selector for range selection in which I could set different drawables if the date is the first selected, last selected, or in the middle. Since the decorator doesn’t expose current date in decorate how can I achieve that. Cheers!

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
sampengillycommented, Oct 24, 2020

That’s essentially all it is. I managed to find that code:

private class RangeSelectionLeftDecorator(
        private val leftDate: LocalDate,
        private val leftDrawable: Drawable
) : DayViewDecorator {

    override fun shouldDecorate(day: CalendarDay): Boolean {
        return leftDate == day.date
    }

    override fun decorate(view: DayViewFacade) {
        view.setSelectionDrawable(leftDrawable)
        view.addSpan(ForegroundColorSpan(Color.WHITE))
    }

}

private class RangeSelectionMiddleDecorator(
        private val middleDates: Set<LocalDate>,
        private val middleDrawable: Drawable
) : DayViewDecorator {

    override fun shouldDecorate(day: CalendarDay): Boolean {
        return day.date in middleDates
    }

    override fun decorate(view: DayViewFacade) {
        view.setSelectionDrawable(middleDrawable)
        view.addSpan(ForegroundColorSpan(Color.BLACK))
    }

}

private class RangeSelectionRightDecorator(
        private val rightDate: LocalDate,
        private val rightDrawable: Drawable
) : DayViewDecorator {

    override fun shouldDecorate(day: CalendarDay): Boolean {
        return rightDate == day.date
    }

    override fun decorate(view: DayViewFacade) {
        view.setSelectionDrawable(rightDrawable)
        view.addSpan(ForegroundColorSpan(Color.WHITE))
    }

}

The color spans were to adjust text color from memory. It’s been a while since I’ve looked at this.

Left Drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="center_vertical|right" android:left="@dimen/cal_tile_height_half">
        <shape android:shape="rectangle">
            <solid android:color="@color/[surface color]"/>
        </shape>
    </item>
    <item android:gravity="center">
        <shape android:shape="oval">
            <solid android:color="@color/[decorator color]"/>
            <size android:height="@dimen/cal_tile_height" android:width="@dimen/cal_tile_height"/>
        </shape>
    </item>
</layer-list>

Middle drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="center">
        <shape android:shape="rectangle">
            <solid android:color="@color/[surface color]"/>
        </shape>
    </item>
</layer-list>

Right drawable:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:gravity="center_vertical|left" android:right="@dimen/cal_tile_height_half">
        <shape android:shape="rectangle">
            <solid android:color="@color/[surface color]"/>
        </shape>
    </item>
    <item android:gravity="center">
        <shape android:shape="oval">
            <solid android:color="@color/[decorator color]"/>
            <size android:width="@dimen/cal_tile_height" android:height="@dimen/cal_tile_height"/>
        </shape>
    </item>
</layer-list>
0reactions
sampengillycommented, Oct 26, 2020

See the applyForSelection() method in the earlier comment. Create the RangeSelectionDecorator and keep a reference to it, then call that method whenever the selection changes on the calendar. It’s a bit of a misnomer as it’s really more of a helper class

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set same drawable selector xml for more than 2 views
I use the following code in my project for many buttons with the selector and other attributes that are the same.
Read more >
Drawable resources - Android Developers
A StateListDrawable is a drawable object defined in XML that uses a several different images to represent the same graphic, depending on the ......
Read more >
Drawables | CodePath Android Cliffnotes
Overview. A drawable resource is a general concept for a graphic that can be drawn to the screen. Drawables are used to define...
Read more >
Android Drawable with Custom States | by Brian Terczynski
By using a StateListDrawable and defining custom states, we can easily show different images within the same View based on states that we...
Read more >
Android Button Design, Custom Button, Round Button, Color
A selector is used to define a different behaviour for different states of the Button. What are drawable states? Each of the following...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found