Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement change black/light theme feature in Android app

Tags:

android

I want to have a "change theme" feature in my app. If I call setTheme() in onCreate(), there is one problem.

The moment after I launch my app, a plain white background appears for a second (because I have set light theme in the manifest). After that, the complete layout of my Activity is displayed - it is either with white or black background, according to user's theme preference.

Is there any way I can change whether white or black background appears after launch?

like image 801
fhucho Avatar asked Aug 31 '10 18:08

fhucho


People also ask

How do I apply a Dark theme to all apps on Android?

Turn Dark theme on or off in your phone's settings Important: When you turn on Dark theme for your phone, many apps also use Dark theme. On your phone, open the Settings app. Tap Display. Turn Dark theme on or off.

How do I change the light Theme in Android Studio?

Take a look at the colors defined in the default theme. In Android Studio, open themes. xml (app > res > values > themes > themes. xml).


1 Answers

Make sure you call setTheme() in onCreate() BEFORE calling setContentView(). Then if you want to dynamically change the theme again later, you should simply restart your activity.

like image 132
Will Tate Avatar answered Oct 05 '22 23:10

Will Tate