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

Add TextView.updateCompoundDrawable... functions

Open tomaszrykala opened this issue 7 years ago • 19 comments

Addresses https://github.com/android/android-ktx/issues/147

tomaszrykala avatar Feb 05 '18 23:02 tomaszrykala

Comments addressed, thanks @romainguy

tomaszrykala avatar Feb 06 '18 00:02 tomaszrykala

@tomaszrykala shouldn't those extensions be placed under androidx.widget package?

szymanskip avatar Feb 06 '18 08:02 szymanskip

Yes, good catch. This should be in androidx.widget.

romainguy avatar Feb 06 '18 09:02 romainguy

Thanks @szymanskip . Corrected @romainguy .

tomaszrykala avatar Feb 06 '18 22:02 tomaszrykala

@romainguy Is there anything still missing in this PR?

tomaszrykala avatar Feb 15 '18 23:02 tomaszrykala

Sorry, I completely forgot about this PR, my bad! Looking at it again, I think this extension should work like ViewGroup.updateMargins. For instance if calling:

udpateCompoundDrawables(left=whatever, right=whatever)

Then top and bottom should remain unchanged instead of becoming null. What do you think?

romainguy avatar Feb 16 '18 05:02 romainguy

@romainguy I could try that, although my original intention for this change was to remove the boilerplate of additional drawables to set when you really want to set one, via default arguments.

What if we have two sets of updateCompoundDrawable... methods :

  • updateCompoundDrawable... - behaving like the ones in this PR
  • updateCompoundDrawablePreserving... - that preserves any existing values

Option two: allow additional Boolean argument, that would toggle this preserve/overwrite behaviour, eg.

fun TextView.updateCompoundDrawables(
    start: Drawable? = null,
    top: Drawable? = null,
    end: Drawable? = null,
    bottom: Drawable? = null,
    keepExisting: Boolean = false
) {
    // if (keepExisting) ...
    setCompoundDrawables(start, top, end, bottom)
}

What do you think?

tomaszrykala avatar Feb 16 '18 17:02 tomaszrykala

Neither of those behaviors are intuitive for a method named "update". I agree that this method should only alter the drawables which are supplied. This is also solves your case of only setting a subset, as they default to not present anyway.

JakeWharton avatar Feb 16 '18 17:02 JakeWharton

It's easy enough to do this with methods that take drawables as arguments eg:

fun TextView.updateCompoundDrawables(
    start: Drawable? = compoundDrawables[0],
    top: Drawable? = compoundDrawables[1],
    end: Drawable? = compoundDrawables[2],
    bottom: Drawable? = compoundDrawables[3]
) {
    setCompoundDrawables(start, top, end, bottom)
}

but how to do this with methods that take @DrawableRes Ints in? , meaning how do I ensure that drawables aren't being overwritten? Call the methods that take in Drawables internally instead? In code like the follwing the getDrawable() calls are cause @Deprecated warnings, and theme one that takes Theme as an argument is from API 21.

/**
 * Updates this TextView's Drawables. This version of the method allows using named parameters
 * to just set one or more Drawables. Use 0 if you do not want a Drawable there.
 *
 * @see TextView.setCompoundDrawablesWithIntrinsicBounds
 */
fun TextView.updateCompoundDrawablesWithIntrinsicBounds(
        @DrawableRes start: Int = -1,
        @DrawableRes top: Int = -1,
        @DrawableRes end: Int = -1,
        @DrawableRes bottom: Int = -1
) {
    setCompoundDrawablesWithIntrinsicBounds(
            if (start == -1) compoundDrawables[0] else if (start == 0) null else resources.getDrawable(start),
            if (top == -1) compoundDrawables[1] else if (top == 0) null else resources.getDrawable(top),
            if (end == -1) compoundDrawables[2] else if (end == 0) null else resources.getDrawable(end),
            if (bottom == -1) compoundDrawables[3] else if (bottom == 0) null else resources.getDrawable(bottom)
    )

tomaszrykala avatar Feb 16 '18 22:02 tomaszrykala

Yep. That's what TextView does except we'll use ContextCompat.getDrawable.

JakeWharton avatar Feb 16 '18 22:02 JakeWharton

Thanks @JakeWharton . I kept thinking of DrawableCompat.

tomaszrykala avatar Feb 16 '18 22:02 tomaszrykala

Done @romainguy @JakeWharton

tomaszrykala avatar Feb 16 '18 23:02 tomaszrykala

Any more work required?

tomaszrykala avatar Feb 21 '18 23:02 tomaszrykala

Rebased on latest master.

tomaszrykala avatar Apr 13 '18 23:04 tomaszrykala

What's left to do here?

oldergod avatar May 15 '18 13:05 oldergod

Can I have an update from the maintainers please if there's anything left to do?

tomaszrykala avatar May 15 '18 18:05 tomaszrykala

4 months later still nothing. A bit disappointing :(

tomaszrykala avatar Jun 11 '18 21:06 tomaszrykala

Sorry Tomasz, nothing left to do at the moment, I just need to find a bit of time to play with the change

romainguy avatar Jun 11 '18 22:06 romainguy

Hi Tomasz,

Sorry but I'm unclear why I have an email from you. I'm too busy at moment with a bug in the Intel CPU architecture.

It affects nearly every computer on the planet. See google project zero & press reports about CPU design flaw.

Regards, Dave On 11 Jun 2018 22:50, "Tomasz Rykała" [email protected] wrote:

4 months later still nothing. A bit disappointing :(

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/android/android-ktx/pull/159#issuecomment-396398732, or mute the thread https://github.com/notifications/unsubscribe-auth/AiqCH1-uSAamIhBAmDxvncU7SiWH8ONwks5t7uY1gaJpZM4R6R3V .

DaveMellor avatar Jun 11 '18 22:06 DaveMellor