Skip to content

Instantly share code, notes, and snippets.

@davidelp68
davidelp68 / MainActivity_nd3_3.kt
Created January 3, 2022 08:27
Android Studio - Night/Day
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 January 3, 2022 08:26
Android Studio - Night/Day
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 January 3, 2022 08:18
Android Studio - Night/Day
val mode = AppCompatDelegate.getDefaultNightMode()
sharedPrefsEdit.putInt("NightMode", mode)
sharedPrefsEdit.apply()
@davidelp68
davidelp68 / MainActivity_nd2.kt
Created January 2, 2022 15:00
Android Studio - Night/Day
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 January 2, 2022 14:51
Android Studio - Night/Day
<?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 January 2, 2022 10:29
Android Studio - Night/Day
<?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 January 2, 2022 10:28
Android Studio - Night/Day
<item name="android:windowBackground">@color/backgroundwindow</item>
@davidelp68
davidelp68 / ActivityMain_nd1_5.kt
Created January 2, 2022 10:14
Android Studio - Night/Day
//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 January 2, 2022 10:09
Android Studio - Night/Day
//Imposta il tema in base all'impostazione di sistema
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
@davidelp68
davidelp68 / ActivityMain_nd1_3.kt
Created January 2, 2022 10:07
Android Studio - Night/Day
//Imposta il tema chiaro
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)