[Android/Kotlin] darkmode ์ ์šฉํ•˜๊ธฐ

2020. 12. 31. 23:21ใ†๐Ÿ“ฑAndroid/๐Ÿ“ Android Study

๋ฐ˜์‘ํ˜•

์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ darkmode(์–ด๋‘์šด ํ…Œ๋งˆ)๋Š” Android 10 (API ๋ ˆ๋ฒจ 29) ์ด์ƒ์—์„œ ์ œ๊ณต๋˜์–ด์ง€๋Š” ๊ธฐ๋Šฅ ์ค‘ ํ•˜๋‚˜์ธ๋ฐ์š”.

์˜ค๋Š˜์€ ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ๋‹คํฌ๋ชจ๋“œ๋ฅผ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด์„œ ์†Œ๊ฐœํ•ด๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ activity_main.xml ํ™”๋ฉด ๊ตฌ์„ฑ

Light ๋ชจ๋“œ / Dark ๋ชจ๋“œ / System ์„ค์ •์— ๋”ฐ๋ผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ฒŒ RadioButton์„ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@color/color_dark_ffffff_0000000"
    android:id="@+id/cl_background">

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ํ…Œ๋งˆ ์„ค์ •"
        android:textSize="20sp"
        android:textColor="@color/color_dark_000000_ffffff"
        app:layout_constraintVertical_bias="0.3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

   <RadioGroup
       android:id="@+id/radioMode"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       app:layout_constraintVertical_bias="0.2"
       app:layout_constraintTop_toBottomOf="@id/txtTitle"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintBottom_toBottomOf="parent">
       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Light"
           android:textColor="@color/color_dark_000000_ffffff"
           android:buttonTint="@color/colorAccent"
           android:id="@+id/rbLight"/>
       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Dark"
           android:buttonTint="@color/colorAccent"
           android:textColor="@color/color_dark_000000_ffffff"
           android:id="@+id/rbDark"/>
       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Default"
           android:buttonTint="@color/colorAccent"
           android:textColor="@color/color_dark_000000_ffffff"
           android:id="@+id/rbDefault"/>
   </RadioGroup>


</androidx.constraintlayout.widget.ConstraintLayout>

 

text์˜ ์ƒ‰๊ณผ activity ๋ฐฐ๊ฒฝ์ƒ‰์ด dark/light ๋ชจ๋“œ์— ๋”ฐ๋ผ ์ƒ‰์„ ๋ณ€๊ฒฝํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š” night์˜ color.xml์ด ์ถ”๊ฐ€๋กœ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค!

 

โœ”๏ธvalues-night ํด๋” ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ

values ์˜ค๋ฅธ์ชฝ ํด๋ฆญ -> New -> Directory -> .../app/src/main/res/values-night ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ

 

โœ”๏ธcolors.xml (night) ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ

values ์˜ค๋ฅธ์ชฝ ํด๋ฆญ -> New -> Values Resource File 

์ด๋•Œ Directory name์„ values-night๋กœ , File name์„ color๋กœ ์„ค์ •ํ•ด์ฃผ์„ธ์š”!

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color_dark_000000_ffffff">#000000</color>
    <color name="color_dark_ffffff_000000">#ffffff</color>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
</resources>

night/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="color_dark_000000_ffffff">#ffffff</color>
    <color name="color_dark_ffffff_000000">#000000</color>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
</resources>

colors.xml ์—์„œ์™€ ๋™์ผํ•œ name์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๊ณ , darkmode์‹œ์— ์›ํ•˜๋Š” ์ƒ‰์„ ์ง€์ •ํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ €๋Š” name๋ช…์„ ์•Œ์•„๋ณด๊ธฐ ์‰ฝ๊ฒŒ {color_dark_light๋ชจ๋“œ์ผ๋•Œ์ƒ‰์ƒ_dark๋ชจ๋“œ์ผ๋•Œ์ƒ‰์ƒ} ์œผ๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๐Ÿ“Œ MainActivity.kt

๊ฐ๊ฐ์˜ Radio ๋ฒ„ํŠผ์„ ํด๋ฆญ์‹œ ์•ฑ์ด light/dark ๋ชจ๋“œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋„๋ก Radio ๋ฒ„ํŠผ Check ์ด๋ฒคํŠธ๋ฅผ ๋žŒ๋‹ค์‹์œผ๋กœ ๊ตฌํ˜„ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

radioMode.setOnCheckedChangeListener { _, checkedId ->
            when(checkedId) {
                R.id.rbLight -> {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
                }
                R.id.rbDark -> {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
                }
                R.id.rbDefault -> {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
                    } else {
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)
                    }
                }
            }
        }

Light ๋ชจ๋“œ ์„ค์ •

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)

Dark ๋ชจ๋“œ ์„ค์ •

AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

์‹œ์Šคํ…œ ์„ค์ •์— ๋”ฐ๋ผ

API 29 ๋ถ€ํ„ฐ Darkmode๊ฐ€ ์ง€์›๋˜๋ฏ€๋กœ Version์— ๋”ฐ๋ผ ์„ค์ •์„ ๋‹ค๋ฅด๊ฒŒ ํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค!

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
	//APP SDK ๋ฒ„์ „์ด Q๋ณด๋‹ค ํด ๋•Œ (API 29 ์ด์ƒ)
	AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
} else {
	//APP SDK ๋ฒ„์ „์ด Q๋ณด๋‹ค ์ž‘์„ ๋•Œ (API 29 ๋ฏธ๋งŒ)
	AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)
}

 

 

๐Ÿ“Œ ๊ตฌํ˜„ ํ™”๋ฉด

 

 

 

 

์ฐธ๊ณ 

 

Android ๋‹คํฌ ๋ชจ๋“œ ์ ์šฉํ•˜๊ธฐ

๋ชฉ์ฐจOverview0. ์‹œ์ž‘ํ•˜๊ธฐ ์ „1. ํ…Œ๋งˆ ๊ถŒ์žฅ ์‚ฌํ•ญ2. ํ…Œ๋งˆ ์„ ํƒ ๋‹ค์ด์–ผ๋กœ๊ทธ ๋งŒ๋“ค๊ธฐ3. ๋‹คํฌ ๋ชจ๋“œ ํ…Œ๋งˆ ์„ค์ •3-1. ์ƒ‰์ƒ3-2. ์ด๋ฏธ์ง€4. ๋‹คํฌ ๋ชจ๋“œ๋ฅผ ์ ์šฉํ•œ ๋ธŒ๋žœ๋””5. ๊ฒฐ๋ก 

labs.brandi.co.kr

 

๋ฐ˜์‘ํ˜•