Skip to content

Instantly share code, notes, and snippets.

@davidelp68
davidelp68 / MainActivity_nd3_3.kt
Created Jan 3, 2022
Android Studio - Night/Day
View MainActivity_nd3_3.kt
package com.dm.tutorialdaynight3
import android.content.Context
import android.content.SharedPreferences //Importazione della Classe SharedPreferences
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.RadioButton
import android.widget.TextView
import androidx.appcompat.app.AppCompatDelegate //Importazione della Classe AppCompactDelegate
@davidelp68
davidelp68 / MainActivity_nd3_2.kt
Created Jan 3, 2022
Android Studio - Night/Day
View MainActivity_nd3_2.kt
val appSettingPrefs: SharedPreferences = getSharedPreferences("AppSettingPrefs", Context.MODE_PRIVATE)
val sharedPrefsEdit: SharedPreferences.Editor = appSettingPrefs.edit()
val isNightMode = appSettingPrefs.getInt("NightMode", -1)
/*MODE_NIGHT_FOLLOW_SYSTEM = -1
* MODE_NIGHT_AUTO = 0 (deprecated)
* MODE_NIGHT_NO = 1
* MODE_NIGHT_YES = 2
* MODE_NIGHT_AUTO_BATTERY = 3*/
when (isNightMode) {
@davidelp68
davidelp68 / MainActivity_nd3_1.kt
Created Jan 3, 2022
Android Studio - Night/Day
View MainActivity_nd3_1.kt
val mode = AppCompatDelegate.getDefaultNightMode()
sharedPrefsEdit.putInt("NightMode", mode)
sharedPrefsEdit.apply()
@davidelp68
davidelp68 / MainActivity_nd2.kt
Created Jan 2, 2022
Android Studio - Night/Day
View MainActivity_nd2.kt
package com.dm.tutorialdaynight2
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.RadioButton
import androidx.appcompat.app.AppCompatDelegate //Importazione della Classe AppCompactDelegate
class MainActivity : AppCompatActivity() {
private var rbDay: RadioButton? = null
@davidelp68
davidelp68 / activity_main_nd2.xml
Created Jan 2, 2022
Android Studio - Night/Day
View activity_main_nd2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
@davidelp68
davidelp68 / styles_nd1_2.xml
Created Jan 2, 2022
Android Studio - Night/Day
View styles_nd1_2.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@color/backgroundwindow</item>
</style>
@davidelp68
davidelp68 / styles_nd1_1.xml
Created Jan 2, 2022
Android Studio - Night/Day
View styles_nd1_1.xml
<item name="android:windowBackground">@color/backgroundwindow</item>
@davidelp68
davidelp68 / ActivityMain_nd1_5.kt
Created Jan 2, 2022
Android Studio - Night/Day
View ActivityMain_nd1_5.kt
//Imposta il tema in base all'attivazione della modalità risparmio energia
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)
@davidelp68
davidelp68 / ActivityMain_nd1_4.kt
Created Jan 2, 2022
Android Studio - Night/Day
View ActivityMain_nd1_4.kt
//Imposta il tema in base all'impostazione di sistema
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
@davidelp68
davidelp68 / ActivityMain_nd1_3.kt
Created Jan 2, 2022
Android Studio - Night/Day
View ActivityMain_nd1_3.kt
//Imposta il tema chiaro
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)