Is it possible to implement dark mode on multiple activities with a single toggle switch ? Or it is only possible with the help of fragments.
Please bear with me, I am new to Android development and a total beginner.
Is it possible to implement dark mode on multiple activities with a single toggle switch ? Or it is only possible with the help of fragments.
Please bear with me, I am new to Android development and a total beginner.
Yes of course, you can handle dark mode for all the fragments in your app easily like this :
//for dark mode
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
// for removing dark mode
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
and if you see there is no change in fragments after changing to dark mode that is because you have defined a color in your fragment and you haven't define a color for dark mode in res>values>colors. for this easily go to res>values>colors and there Right Click> New> values Resource Files > choose a name and in Available Qualifiers choose night mode, now you will have a colors.xml file and a colors.xml(night) file which have colors with same names but different values specific for that mode(Dark/normal)
A tricky solution is to use a static boolean variable in any of the class. Initially,
public static boolean darkMode = false;
If the DARK MODE button is pressed, set
NameOfTheClass.darkMode = true;
In the onCreate() of all the activities, if darkMode == true then set the background color black.
You could store the user's preferences and load them when the app is launched. For example as a public static boolean and just call in every activity or make a CustomAppCompatActivity class which does the job for you. One way or another. You've got to be creative.