Search code examples
androidtextcolor

Failed to set textColor in Android on some devices


I have a game which I need to change the color of the text according to users inputs

I change the text in 2 places using different methods and both methods fail on some devices and textColor always become black with white stroke on those devices , however I never add stroke on my code anywhere, devices reported so far is Samsung galaxy prime, Xiaomi Mi A2 Lite, Huawei P20 Pro, however I tried the game on Samsung galaxy prime a friend of mine have and it worked perfect, so I'm unable to generate this issue on a testing device I have

Picture showing the issue, all text is black with white stroke color

enter image description here

normal picture

enter image description here

First method in the keyboard section my code is as below

btns[t].setTextColor(ContextCompat.getColor(getContext(),R.color.text_color));

and when user set night mode I change it to be

btns[t].setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_night));

where colors is text_color = #000000 / text_color_night = #ffffff

Second method in the play board section , my code is

private final Paint selectedBox = new Paint();

private final TextPaint letterTextNormal = new TextPaint();
private final TextPaint letterTextCorrect = new TextPaint();
private final TextPaint letterTextWrong = new TextPaint();

public PlayboardRenderer(Context con) {
    letterTextNormal.setTextAlign(Align.CENTER);
    letterTextNormal.setAntiAlias(true);
    letterTextNormal.setTypeface(((MainActivity) con).getSelectedFont());

    letterTextCorrect.setTextAlign(Align.CENTER);
    letterTextCorrect.setAntiAlias(true);
    letterTextCorrect.setTypeface(((MainActivity) con).getSelectedFont());

    letterTextWrong.setTextAlign(Align.CENTER);
    letterTextWrong.setAntiAlias(true);
    letterTextWrong.setTypeface(((MainActivity) con).getSelectedFont());
}

private void drawBox(Canvas canvas, int x, int y, int row, int col, float scale, Box box, Word currentWord) {
    int boxSize = (int) (BOX_SIZE * scale);

    // scale paints
    float textSize = (boxSize) - (boxSize / 5);
    letterTextNormal.setTextSize(textSize);
    letterTextCorrect.setTextSize(textSize);
    letterTextWrong.setTextSize(textSize);

        if (!box.isBlank() && !box.isEmpty())
            if (box.getState() == Box.STATE.NORMAL) {
                canvas.drawText(Character.toString(box.getChar()), x + (boxSize / 2), y + (int) (textSize * 0.9), letterTextNormal);
            } else if (box.getState() == Box.STATE.WRONG) {
                canvas.drawText(Character.toString(box.getChar()), x + (boxSize / 2), y + (int) (textSize * 0.9), letterTextWrong);
            } else if (box.getState() == Box.STATE.CORRECT) {
                canvas.drawText(Character.toString(box.getChar()), x + (boxSize / 2), y + (int) (textSize * 0.9), letterTextCorrect);
            }
    }
}

Solution

  • After finally getting my hand on a device that has this issue I found that it has turned on option in device Settings>Accessibility>High contrast text

    If user turned on this option it forces all TextViews textColor to be either black or white

    So you can check in code if user have this option on or not and act accordingly using below code

    /**
     * Returns if the high text contrast in the system is enabled.
     * <p>
     * <strong>Note:</strong> You need to query this only if you application is
     * doing its own rendering and does not rely on the platform rendering pipeline.
     * </p>
     *
     * @return True if high text contrast is enabled, false otherwise.
     *
     * @hide
     */
    public boolean isHighTextContrastEnabled() {
        synchronized (mLock) {
            IAccessibilityManager service = getServiceLocked();
            if (service == null) {
                return false;
            }
            return mIsHighTextContrastEnabled;
        }
    }
    

    So in your code, you can access this method by doing so

    AccessibilityManager am = (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);
    boolean isHighTextContrastEnabled = am.isHighTextContrastEnabled();