Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to change theme during app startup

Tags:

android

Currently, I do provide 2 theme in my app, based on user last choice - dark theme and light theme.

During main activity start-up, I will do the following.

public class MyFragmentActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // getUserLastThemeChoice will either return R.style.Theme_My_Light or 
        // R.style.Theme_My_Dark.
        this.setTheme(getUserLastThemeChoice());

        super.onCreate(savedInstanceState);

In my AndroidManifest.xml, I have the following

<application
    ...
    android:theme="@style/Theme.My.Dark" >

Due to the hard-coded value in AndroidManifest.xml, if user previous choice is light theme, I will observed the following.

  1. App start up with dark theme, due to AndroidManifest.xml
  2. A while...
  3. App will change to white theme due to this.setTheme in main Activity

In there any way I can avoid such transition observation? That's it, just present to user directly, based on their last choice.

p/s Note, it wouldn't help much if you remove the theme information from manifest. As the system is going to use holo dark by default. You will still observe the transition, especially in slow device.

like image 488
Cheok Yan Cheng Avatar asked Feb 20 '14 16:02

Cheok Yan Cheng


People also ask

How to change current theme at runtime in my Android app?

This example demonstrates how do I change current theme at runtime in my android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.

What happens when I change the default theme during runtime?

It will change the current page default theme light to dark theme. There is a problem while changing the whole app theme during runtime. If you try to change it during runtime, you’ll get an exception.

Is it possible to change the theme of the application?

You can change it from xaml also, if you change the theme in App.xaml file it will affect the whole application theme. But sometimes we need to give the user the ability to change the theme so in that case we need to handle it in the code and change the theme in runtime.

How to change the light theme programmatically in Windows 10?

We could easily change it programmatically. In Windows 10 by defaultthe Light theme is appliedand you can check it from App.xaml file, it looks like the following image. You can change it from xaml also, if you change the theme in App.xaml file it will affect the whole application theme.


2 Answers

Hiding theme manipulation, particularly on app start up is tricky. Unfortunately there is no way to consistently remove that initial transition, because there's no telling how fast/slow the device running it is. Android will always show that blank background in the default theme before inflating the layout for the main activity.

But there is a trick to hiding it. A splash screen that appears when the user opens the app, and fades out after the initial activity has loaded. This way the splash screen hides the theme transition, without jarring the user. Splash screens receive some well deserved hate, but a situation like this does justify their use. Again, I don't know the specifics of your app, but you have to weigh the pros/cons of having a splash screen and smooth transition vs. no splash screen and a jarring transition.

There are loads of ways of implementing splash screens. In your case I would try to create a launcher activity, with a neutral theme, and apply a smooth animation for the transition between the splash screen and your main activity like so:

startActivity( new Intent( LauncherActivity.this, MainActivity.class ) );
overridePendingTransition( R.anim.fade_in, R.anim.fade_out );

And the animations:

<!-- fadeout -->
<?xml version="1.0" encoding="UTF-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="1.0" android:toAlpha="0.0"
android:duration="300" />

<!-- fadein -->
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="300" />
like image 82
Pete Avatar answered Oct 11 '22 23:10

Pete


I know this is very old but maybe it still helps:

If you set the activity's theme to

android:theme="@android:style/Theme.Translucent.NoTitleBar"

it'll not show that initial screen. Instead it'll just take a bit more for the app to show anything at all.

like image 34
joaomgcd Avatar answered Oct 11 '22 22:10

joaomgcd