Implementing the function of changing black / light themes in an Android application

I want to have the "change theme" feature in my application. If I call setTheme () on onCreate (), one problem arises.

The moment I launch the application, a plain white background appears for a second (because I set the light theme in the manifest). After that, the full layout of my activity is displayed - either with a white or black background, in accordance with the preference of the user's theme.

Is there any way to change if a white or black background appears after launch?

+16
android
Aug 31 '10 at 18:31
source share
3 answers

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

+9
Jan 19 '11 at 17:07
source share

If you add a theme to the entire program, how can you start with it:

In the manifest, you add to your application tag that you are using a theme.

 <application android:theme="@style/mythemename"> 

Then look at the XML Theme to make sure that you have what you need to declare in the appropriate places.

If for a specific action you can add an activity tag

 <activity android:theme="@android:style/Theme.propertyname"> 

You can also, if you want your theme to simply change the background color, follow the same template with an activity or application tag (which is ever used) and set the name of the colorbackground element to whatever you want,

You can also use the XML Theme and redo what you want in your current theme and invoke this custom theme using the method above.

I hope this helps, and if you don’t tell me, so that I can better help in the future.

+1
Sep 09 '10 at 20:45
source share

Another way would be to have a splash screen that would check, for example, a preference variable, and then decide whether to use a light or dark theme. This way you can also use XML layouts.

EDIT: Another way is for the entire layout to define material in the onCreate () method, and then just run the onStart () method when ready.

0
Sep 06 '10 at 16:05
source share



All Articles