Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore High Contrast Accessibility option in Android during canvas drawing?

Tags:

android

canvas

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.

like image 209
naixx Avatar asked Nov 16 '22 15:11

naixx


1 Answers

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

like image 107
mlebedy Avatar answered Jun 16 '23 19:06

mlebedy