Android - Prevent white screen at startup
The problem with white background is caused because of android's cold start while the app loads to memory, and it can be avoided with this:
public class OnboardingWithCenterAnimationActivity extends AppCompatActivity {
public static final int STARTUP_DELAY = 300;
public static final int ANIM_ITEM_DURATION = 1000;
public static final int ITEM_DELAY = 300;
private boolean animationStarted = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onboarding_center);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (!hasFocus || animationStarted) {
return;
}
animate();
super.onWindowFocusChanged(hasFocus);
}
private void animate() {
ImageView logoImageView = (ImageView) findViewById(R.id.img_logo);
ViewGroup container = (ViewGroup) findViewById(R.id.container);
ViewCompat.animate(logoImageView)
.translationY(-250)
.setStartDelay(STARTUP_DELAY)
.setDuration(ANIM_ITEM_DURATION).setInterpolator(
new DecelerateInterpolator(1.2f)).start();
for (int i = 0; i < container.getChildCount(); i++) {
View v = container.getChildAt(i);
ViewPropertyAnimatorCompat viewAnimator;
if (!(v instanceof Button)) {
viewAnimator = ViewCompat.animate(v)
.translationY(50).alpha(1)
.setStartDelay((ITEM_DELAY * i) + 500)
.setDuration(1000);
} else {
viewAnimator = ViewCompat.animate(v)
.scaleY(1).scaleX(1)
.setStartDelay((ITEM_DELAY * i) + 500)
.setDuration(500);
}
viewAnimator.setInterpolator(new DecelerateInterpolator()).start();
}
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?colorPrimary"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="144dp"
tools:ignore="HardcodedText"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:alpha="0"
android:text="Hello world" android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
android:textColor="@android:color/white"
android:textSize="22sp"
tools:alpha="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="8dp"
android:alpha="0"
android:gravity="center"
android:text="This a nice text"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle.Inverse"
android:textSize="20sp"
tools:alpha="1"
/>
<Button
android:id="@+id/btn_choice1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:scaleX="0"
android:scaleY="0"
android:text="A nice choice"
android:theme="@style/Button"
/>
<Button
android:id="@+id/btn_choice2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:scaleX="0"
android:scaleY="0"
android:text="Far better!"
android:theme="@style/Button"
/>
</LinearLayout>
<ImageView
android:id="@+id/img_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/img_face"
tools:visibility="gone"
/>
</FrameLayout>
img face
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="?colorPrimary"/>
<item>
<bitmap
android:gravity="center"
android:src="@drawable/img_face"/>
</item>
Add this theme to your splashscreen in the manifest
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowBackground">@null</item>
</style>
<style name="AppTheme.CenterAnimation">
<item name="android:windowBackground">@drawable/ll_face_logo</item>
</style>
which will produce efect like this
for more details and more solutions you can check this
BlogPost
How To fix white screen on app Start up?
Just mention the transparent theme to the starting activity in the AndroidManifest.xml
file.
Like:
<activity
android:name="first Activity Name"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and extend that screen with Activity
class in place of AppCompatActivity
.
like :
public class SplashScreenActivity extends Activity{
----YOUR CODE GOES HERE----
}
How can I remove white screen which appear before splash screen?
Finally got my answer Splash Screen in Right Way. I do just following.
In values->styles.xml I created splash screen background image
<style name="AppTheme.Splash" parent="AppTheme.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
</style>
For below api 19, in values-19->styles.xml I used
<style name="AppTheme.Splash" parent="AppTheme.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
I removed setContentview()
from SplashActivity and added style for splash screen in Manifest.xml
file android:theme="@style/AppTheme.Splash"
App shows white screen on start, then it starts
Your app is having a white screen at the start because the app waits to get the current theme of the app and then loads your splash screen.
For setting splash screen so the app opens instantly with it, you have to create a new theme inside styles.xml
<style name="SplashScreen" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowBackground">@drawable/splash</item>
<item name="android:statusBarColor">@color/spalshStatusBar</item>
</style>
Create your splash.xml
drawable with your app's icon in the center
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/splashBackground" />
<item
android:drawable="@drawable/ic_app_icon"
android:gravity="center" />
</layer-list>
Set this theme as default in your manifest
application tag
<application
..
android:theme="@style/SplashScreen"
..
..>
Now once your app's theme loading completes, change your app theme accordingly.
What is the reason for white screen on launch of app ? How to avoid it completely?
What is white screen while starting an application ?
The white screen comes when we start an application, then every application has to be placed in phone memory to work. While application, putting it's working component in the memory, Android OS
shows as the white screen or we can say that cold start. That's why if a user with low memory phone opens application, white screen visible for longer time because application is waiting for putting it's component in the memory(i.e. RAM) to work.
How to resolve Cold start ?
Basically, cold starts time is depends on certain parameters. That is,
1) Storage available in user's phone
2) Heavy loading components placed on MainThread
of MainActivity (i.e. Launcher Activity)
Now solutions,
As a developer we can't do anything much about first parameter of cold start, because we can't force user to clear memory every time when he or she open's our application. But, we can optimize our application to perform better on each type of devices either it has low or high memory.
Optimizations for reducing cold start time,
Simply, if you are using heavy view components in your application or you are doing heavy network work on MainThread or either any kinda work, which results in blocking or increasing in render time of your LauncherActivity. Then you have to do it in background or simply, render common components first then load full view in background.
Now, about removing white screen
On the first thought, you can't remove white screen. But you can replace it by either use,
<style name = "AppTheme">
<item name ="android:windowBackground">@color/black</item>
</style>
in your LauncherActivities style.So, if you don't want to AppTheme
color then you can set custom color with above method. Or you can replace white screen with your AppTheme
primaryColor by placing it in onCreate()
method of your activity as,
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme); //it will shows the default color from your apptheme in place of white screen, you can also define your theme in style
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
}
Related Topics
Java - Declaring from Interface Type Instead of Class
How to Close a Java Swing Application from the Code
How to Convert Image to Byte Array in Java
Collections Sort(List<T>,Comparator<? Super T>) Method Example
Does Java Casting Introduce Overhead? Why
How to Implement Enum with Generics
In Java, Are Enum Types Inside a Class Static
How Big Is an Object Reference in Java and Precisely What Information Does It Contain
Is Default No-Args Constructor Mandatory for Gson
How to Make a Jtable Non-Editable
Java Output Formatting for Strings
How to Change Text of a Textview in Navigation Drawer Header
How to Convert Firebase Data to Java Object...
Android Post Picture to Facebook Wall
Java/Android - How to Print Out a Full Stack Trace
Onclick Event Is Not Triggering | Android