I have a custom view that I want to have colored text. But when the high contrast
option is enabled canvas ignores all paint's color settings and draws text as black and white (drawText
overloads)
It is somewhat possible to detect if this option is enabled(via reflection, etc). Is there a way to ignore that for some views?
PS I know about the switch in settings, that's not a solution.
My workaround looks something like this:
public void drawTextWithoutHighContrastEffects(Canvas canvas, String text, float x, float y, Paint paint)
{
if (isHighContrastTextEnabled)
{
Path path = new Path();
paint.getTextPath(text, 0, text.length(), x, y, path);
canvas.drawPath(path, paint);
} else {
canvas.drawText(text, x, y, paint);
}
}
Please note that this is a simplified solution, e.g. path
instance should not be recreated for each draw operation, create once and then reuse it. Also paint
object should be set up correctly with FILL
style in order to make the text drawn using drawPath
look the same as drawn by drawText
.
The value of isHighContrastTextEnabled
flag comes from here: Detect if 'High contrast' is enabled in Android accessibility settings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With