I am developing an android application.
I want to add a feature to let the user to change the application’s theme at the runtime.
I try to use the following statement in my main activity.
setTheme(android.R.style.Theme_Black);
However, it merely change the dialog to black background with white text.
What can I do to change all the componenets’ theme to Black including the action bar and fragment?
For a global runtime change, what could work is extending Application and setting the theme (probably in
onCreate(), since this class also has asetTheme()method on account of it extendingContextWrapper.Make sure to declare your Application class in the manifest:
However, since extending application is usually frowned upon, I’d rather set the theme on a per
Activitybasis since Activities get destroyed and recreated way more often than theApplicationobject. Maybe make a static method that returns the current theme fromSharedPreferences.