saket / Better-Link-Movement-Method

Attempts to improve how clickable links are detected, highlighted and handled in TextView
Apache License 2.0
783 stars 77 forks source link

Please reduce minSdk to 14 #23

Closed AndroidDeveloperLB closed 5 years ago

AndroidDeveloperLB commented 6 years ago

Seems it still works fine...

saket commented 6 years ago

Sure, want to send a PR?

AndroidDeveloperLB commented 6 years ago

Hmmmm... I was wrong. The library uses this:

BackgroundColorSpan highlightSpan = new BackgroundColorSpan(textView.getHighlightColor());

Any alternative to this?

saket commented 6 years ago

You could potentially recreate the span because all it does is set TextPaint#bgColor, but the APIs for ParcelableSpan are internal so it's not a very good option.

At this point of time, it's highly recommended to just use appcompat and bump your minSdk to atleast 21.

AndroidDeveloperLB commented 6 years ago

I think I got something .

How about this:

        int hightlightColor;
        if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
            hightlightColor = textView.getHighlightColor();
        } else {
            try {
                Field highlightPaintField = textView.getClass().getDeclaredField("mHighlightPaint");
                highlightPaintField.setAccessible(true);
                final Paint highlightPaint = (Paint) highlightPaintField.get(textView);
                hightlightColor = highlightPaint.getColor();
            } catch (Exception e) {
                e.printStackTrace();
                final int textColor = textView.getTextColors().getDefaultColor();
                final int linkColor = textView.getLinkTextColors().getDefaultColor();
                final int textAndLinkColorAverage = Color.rgb((Color.red(textColor) + Color.red(linkColor)) / 2,
                        (Color.green(textColor) + Color.green(linkColor)) / 2,
                        (Color.blue(textColor) + Color.blue(linkColor)) / 2);
                if (textAndLinkColorAverage == textColor || textAndLinkColorAverage == linkColor)
                    hightlightColor = Color.rgb(255 - Color.red(textAndLinkColorAverage),
                            255 - Color.green(textAndLinkColorAverage),
                            255 - Color.blue(textAndLinkColorAverage));
                else hightlightColor = textAndLinkColorAverage;
            }
        }
        BackgroundColorSpan highlightSpan = new BackgroundColorSpan(hightlightColor);
saket commented 6 years ago

Ah, I thought it was BackgroundColorSpan that's only available on 21+.

I don't think I want to include this workaround in the library. Do you want to maintain a fork that backports this library to 14?

AndroidDeveloperLB commented 6 years ago

Are you sure? It seems to work very well now.

And, I've also made the sample to work, even though it was set for some reason to work only from API 23 instead of 16 ...

Here:

https://github.com/saket/Better-Link-Movement-Method/pull/24 https://github.com/AndroidDeveloperLB/Better-Link-Movement-Method

saket commented 6 years ago

Yep. I strongly think using minSdk as 21 is the way to go forward, unless you really want to target ~6 year old devices.

This solution may work, but using reflection is always a hacky solution. Sorry :)

AndroidDeveloperLB commented 6 years ago

It's not targetting. It's minSdk. You can target latest API, as I did : 28 .

The support library is from API 14, as the statistics show that they are still there: https://developer.android.com/about/dashboards/ So if Google supports from API 14, other libraries should too, when possible.

Reflection is what I did, but it also has a fallback. So, very small chance of issues.

saket commented 5 years ago

Closing this because I still don't think this library should maintain workarounds to support old devices.

AndroidDeveloperLB commented 5 years ago

It's ok. I've deleted it now, because all apps are from API 16 now, including some stuff of Firebase/Google.