There are just two things that need to be done here:įirstly, set app:useSimpleSummaryProvider attribute value to true in ListPreference. How do we fix that? Well, that's very simple actually. As you can see, the ListPreference doesn't show which option is currently selected in the summary. For example, drawable-night or values-night.Īnd with this, we've successfully got our dark mode toggle working. You do so by adding these resources in resource folders with the extension of -night. It is possible to add separate icons, colors, styles, and other resources to be used in the day and the night mode. Now, a provision has to be made to make navigation to the SettingsActivity possible which I did by adding an action to the options menu. Override fun onSharedPreferenceChanged ( sharedPreferences : SharedPreferences ?, key : String ?) Implement SharedPreferences.OnSharedPreferenceChangeListener in SettingsActivity and override the onSharedPreferenceChanged() function. I want to add a transition when user switched from Light to Dark mode and vice verse. You should have two themes.xml files now, One for day mode and one for night mode. Following values can be passed to this method:ĪppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM - This will toggle the night mode based on whether the system-wide dark mode is enabled or not.ĪppCompatDelegate.MODE_NIGHT_NO - Light mode will be enabled.ĪppCompatDelegate.MODE_NIGHT_YES - Dark/Night mode will be enabled.ĪppCompatDelegate.MODE_NIGHT_AUTO_BATTERY - Dark/Night mode will be enabled when the device is on the battery saving mode. Set File name as themes and add qualifier Night Mode. In the activitymain. The AppCompatDelegate class' setDefaultNightMode() function is used to change the night mode settings. Dark System default (the recommended default option) Let’s code First, go ahead, and create a new Android project. Now that we are done with the layout, let's look at the code. Follow System Dark Mode Light Mode Selected Dark Mode Selected Auto Battery Dark Mode MODE_NIGHT_FOLLOW_SYSTEM MODE_NIGHT_NO MODE_NIGHT_YES MODE_NIGHT_AUTO_BATTERY
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |