Android 다크모드 적용
2020. 5. 18. 09:29ㆍAndroid
반응형
1. style - theme의 parent를 Theme.AppCompat.DayNight 로 수정
<style name="AppTheme" parent="Theme.AppCompat.DayNight">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
2. theme의 parent를 DayNight 로 설정하지 못하는 경우에는 android:forceDarkAllowed 속성 추가
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:forceDarkAllowed">true</item>
</style>
3.Kotlin 코드 강제 다크모드
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<View>(R.id.change_mode_btn).setOnClickListener {toggleNightMode() }
renderModeState()
}
fun renderModeState() {
var output: TextView = findViewById(R.id.output_tv)
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) {
output.text = "NIGHT_MODE_OFF"
}
else if (currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
output.text = "NIGHT_MODE_ON"
}
}
fun toggleNightMode() {
val currentNightMode = resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
if (currentNightMode == Configuration.UI_MODE_NIGHT_NO) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
}
else if (currentNightMode == Configuration.UI_MODE_NIGHT_YES) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
}
}
}
출처: https://softwaree.tistory.com/48 [Owl Life]
4.Java 코드 강제 다크모드
int currentNightMode = getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_MASK
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// 야간 모드가 활성화되지 않음
case Configuration.UI_MODE_NIGHT_YES:
// 야간 모드가 활성화되어 있음
case Configuration.UI_MODE_NIGHT_UNDEFINED:
// 어떤 모드인지 알지 못할때
}
반응형
'Android' 카테고리의 다른 글
Android 키보드 올림 내림 상태확인 (0) | 2020.05.25 |
---|---|
Android Dialog(다이얼로그) EditText 커스텀 (0) | 2020.05.20 |
Android EditText inputType 이용한 완료버튼 (0) | 2020.05.20 |
Android Edittext 공백체크 (0) | 2020.05.19 |
Android BadTokenException 해결방법 (0) | 2020.05.18 |