0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Androidアプリでダークモードに対応する

0
Posted at

Androidアプリでダークモードに対応させる方法をメモします。ダークモードとは、背景を暗く・文字を明るく表示するUIテーマのことで、システム設定に応じて自動でアプリの見た目を切り替えることができます。

ダークモード対応の方法

まずはアプリのテーマを「DayNight」に設定します。これによりシステム設定に応じて自動でライト・ダークモードが切り替わるようになります。

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
    <!-- カスタマイズ -->
</style>

カラーリソースを分ける

ダークモードでは色の定義を分けることができます。

res/
 ├ values/colors.xml
 └ values-night/colors.xml

例:

values/colors.xml
<color name="background">#FFFFFF</color>
values-night/colors.xml
<color name="background">#121212</color>

ダークモードに対応するため、色の直書きは避けましょう

android:background="#FFFFFF" // NG
android:background="@color/background" // OK

画像・アイコンの対応

ダークモードでは注意が必要です。白背景前提の画像は避けて、透過PNGやVectorDrawableを使いましょう。
必要に応じて drawable-night を用意すれば自動で切り替え可能です。

res/
 ├ drawable/
 │   └ icon.png
 └ drawable-night/
     └ icon.png

プログラムで制御する

またユーザー設定に関係なく強制的にダークモードにすることも可能です。ただしこの方法はユーザーの意図を無視する挙動になるため注意が必要です。

AppCompatDelegate.setDefaultNightMode(
    AppCompatDelegate.MODE_NIGHT_YES
)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?