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.
AndroidManifest.xml
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.
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.
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.
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.
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.
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" />
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With