Skip to content

Switch modes programatically

Devrath edited this page May 28, 2021 · 2 revisions

Ways of selecting the View Modes

There are three modes which we can programmatically select

  • Dark Mode > Display into dark mode
  • Light Mode - > Display into light mode
  • System -> Default settings from the system

Code to change the models programmatically using day-night theme

AppCompatDelegate.setDefaultNightMode(nightMode)
// nightMode = Integer value passed based on selection

How to persist or save the choice the user has made

  • For this, we use shared preferences to save the selection, it will be integer value we are saving

Handling when we close and open the application

Use get the value in shared preferences and load it when the application opens