5
4

More than 1 year has passed since last update.

Androidの通知アイコンが指定通りの色にならない問題

Last updated at Posted at 2023-01-16

Androidの通知アイコンが指定通りの色にならない

image.png

Androidの通知アイコン(①)が指定した通りの色にならず、なぜか黒ずんでしまうので原因を調べたら、Androidの仕様によるものでした。

val builder = Notification.Builder(context, id)
.setColor(ContextCompat.getColor(context, R.color.colorAccent)) // ←この色が正しく出ない

Androidの仕様が原因?

どうやら、背景色とコンテンツ色のコントラスト比が一定以下だと自動的に補正されるというAndroidの仕様があるらしい。
公式ドキュメントの記述を見つけられなかったが、ソースを見たところでは 4.5 がしきい値のようだ。
WCAGの基準に則しているのだろう。

ContrastColorUtil.java
    /**
     * Finds a text color with sufficient contrast over bg that has the same or darker hue as the
     * original color, depending on the value of {@code isBgDarker}.
     *
     * @param isBgDarker {@code true} if {@code bg} is darker than {@code color}.
     */
    public static int ensureTextContrast(int color, int bg, boolean isBgDarker) {
        return ensureContrast(color, bg, isBgDarker, 4.5);
    }

色のコントラスト比を確認する方法

コントラスト比は https://webaim.org/resources/contrastchecker/ で簡単に確認できた。
image.png

4.5 って、けっこう濃い色になっちゃうよね。

参考

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4