Closed AndroidDeveloperLB closed 5 years ago
Sure, want to send a PR?
Hmmmm... I was wrong. The library uses this:
BackgroundColorSpan highlightSpan = new BackgroundColorSpan(textView.getHighlightColor());
Any alternative to this?
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.
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);
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?
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
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 :)
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.
Closing this because I still don't think this library should maintain workarounds to support old devices.
It's ok. I've deleted it now, because all apps are from API 16 now, including some stuff of Firebase/Google.
Seems it still works fine...