android-ktx icon indicating copy to clipboard operation
android-ktx copied to clipboard

TextView.setCompoundDrawable... with default arguments

Open tomaszrykala opened this issue 7 years ago • 7 comments

eg.

@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
fun TextView.setCompoundDrawablesRelative(
        start: Drawable? = null,
        top: Drawable? = null,
        end: Drawable? = null,
        bottom: Drawable? = null
) {
    setCompoundDrawablesRelative(start, top, end, bottom)
}
// same for the other, similar methods

tomaszrykala avatar Feb 05 '18 22:02 tomaszrykala

Name won't work. Extensions cannot override members.

JakeWharton avatar Feb 05 '18 22:02 JakeWharton

True, thanks. So replace set with update, eg. fun TextView.updateCompoundDrawablesRelative(...) ?

tomaszrykala avatar Feb 05 '18 22:02 tomaszrykala

Yeah. And that's unfortunate. We should do non-relative too.

PRs welcome.

On Mon, Feb 5, 2018 at 5:34 PM Tomasz Rykała [email protected] wrote:

True, thanks. So replace set with update, eg. fun TextView.updateCompoundDrawablesRelative(...) ?

— You are receiving this because you commented.

Reply to this email directly, view it on GitHub https://github.com/android/android-ktx/issues/147#issuecomment-363244600, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEEEXrZJpv__FV90srSiasjiRYV7gm3ks5tR4H1gaJpZM4R6L2x .

JakeWharton avatar Feb 05 '18 22:02 JakeWharton

Will do, thanks.

tomaszrykala avatar Feb 05 '18 22:02 tomaszrykala

The compound drawables extensions could be "var" extensions like this?

var TextView.drawableTop: Drawable?
    get() = compoundDrawables[1]
    set(value) {
        setCompoundDrawables(
            compoundDrawables[0],
            value,
            compoundDrawables[2],
            compoundDrawables[3]
        )
    }

Thanks

fredy-mederos avatar Feb 06 '18 16:02 fredy-mederos

@fredy-mederos That's var TextView.compoundDrawableTop.

consp1racy avatar Mar 04 '18 22:03 consp1racy

@tomaszrykala null is a valid argument for setting compound drawables. Perhaps we should use a marker object.

private object DoNotChange : Drawable() {
    override fun draw(canvas: Canvas) {
    }

    override fun setAlpha(alpha: Int) {
    }

    override fun getOpacity(): Int = PixelFormat.TRANSPARENT

    override fun setColorFilter(colorFilter: ColorFilter?) {
    }
}

fun TextView.updateCompoundDrawablesWithIntrinsicBounds(
        left: Drawable? = DoNotChange,
        top: Drawable? = DoNotChange,
        right: Drawable? = DoNotChange,
        bottom: Drawable? = DoNotChange
) {
    val compoundDrawables = compoundDrawables
    val newLeft = if (left != DoNotChange) left else compoundDrawables[0]
    val newTop = if (top != DoNotChange) top else compoundDrawables[1]
    val newRight = if (right != DoNotChange) right else compoundDrawables[2]
    val newBottom = if (bottom != DoNotChange) bottom else compoundDrawables[3]
    setCompoundDrawablesWithIntrinsicBounds(newLeft, newTop, newRight, newBottom)
}

It's not as fast and pretty, probably not a great candidate for inlining. getCompoundDrawables() involves cloning so we get just one local copy ahead of time.

And do that for all the intrinsic and non-intrinsic and relative and absolute variants, of course.

consp1racy avatar Mar 04 '18 22:03 consp1racy