8 votes

Show SplashScreen while loading the application on Android

I have implemented a splashcreen by a activity who inflates his layout with an image centered, but what I find that if this is the first time you install the app, during the loading/compilation shows a blank screen, more or less 6 seconds, the second time you launch the app directly starts with the SplashScreen.

Graphically as well

The First time you run the app after installation or erase data from the settings menu

(click to open app).........................(splashscreen)------(ready)

The second time you open the app

(click to open app) - (splashscreen)------(ready)

My question is if there is any way to put a splashcreen during that period of pre-charge of Android.

Manifest.xml

<activity
    android:name=".SplashActivity"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:theme="@style/AppTheme.SplashTheme">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

Style AppTheme.SplashTheme the Theme that inherits the activity

<style name="AppTheme.SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
</style>

Layout XML splash_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorSplashScreen">

    <LinearLayout
        android:id="@+id/splashscreen"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:contentDescription="@string/about.alt_logo"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher"
            android:layout_gravity="center"/>

        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:layout_marginTop="8dp" />

    </LinearLayout>

</LinearLayout>

SplashActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;

/*
config_longAnimTime   = 400
config_mediumAnimTime = 300
config_shortAnimTime  = 150
*/
public class SplashActivity extends AppCompatActivity {

    private String TAG = "SplashActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        final int SPLASH_DISPLAY_LENGTH = 1200;
        final int SPLASH_DISPLAY_OFFSET = getResources().getInteger(android.R.integer.config_shortAnimTime);;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);

        Log.d(TAG, "onCreate() called with: " + "savedInstanceState = [" + savedInstanceState + "]");
        final Handler handler = new Handler();

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {


                final LinearLayout img = (LinearLayout) SplashActivity.this.findViewById(R.id.splashscreen);

                Animation fadeOut = new AlphaAnimation(1, 0);
                fadeOut.setInterpolator(new AccelerateInterpolator());
                fadeOut.setDuration(getResources().getInteger(android.R.integer.config_longAnimTime));

                fadeOut.setAnimationListener(new Animation.AnimationListener()
                {
                    public void onAnimationEnd(Animation animation)
                    {
                        img.setVisibility(View.GONE);

                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                //Create an intent that will start the main activity.
                                Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
                                SplashActivity.this.startActivity(mainIntent);

                                //Apply splash exit (fade out) and main entry (fade in) animation transitions.
                                overridePendingTransition(R.anim.zoom_enter, android.R.anim.fade_out);

                                //Finish splash activity so user cant go back to it.
                                SplashActivity.this.finish();

                            }
                        }, SPLASH_DISPLAY_OFFSET);

                    }
                    public void onAnimationRepeat(Animation animation) {}
                    public void onAnimationStart(Animation animation) {}
                });

                img.startAnimation(fadeOut);

                Log.d(TAG, "finish SplashScreen: " + SPLASH_DISPLAY_LENGTH);

            }
        }, SPLASH_DISPLAY_LENGTH);

    }

}

Update: It shows the white screen because it inherits the theme of light, because if I add the style of the theme, it shows a black screen.

    <item name="android:background">#FF000000</item>
    <item name="android:colorBackground">#FF000000</item>

I think that the solution lies in implementing draw the icon by loading the theme before using a layout xml, open the app ---> apply theme ----> load layout in the interface.

6voto

Elenasys Points 67941

When you want to start the home screen of the app (SplashScreen) does not show that annoying white background, I was able to fix it easily by creating a theme which relacionarias to the activity:

 <style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/fondo_splash</item>
   </style>

In reality, the property that defines the background at the start of the activity is android:windowBackground and can even add a picture (@drawable/ )

    <item name="android:windowBackground">@drawable/fondo_splash</item>

or a color (@color/):

    <item name="android:windowBackground">@color/fondo_splash</item>

Inside of our AndroidManifest.xml we can define the theme at the application level :

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

or any specific activity:

<activity android:name=".SplashScreenActivity"
   android:theme="@style/SplashTheme" >

with this always make sure to upload a background (drawable or color), before you load an image or to fully start the application.

introducir la descripción de la imagen aquí

4voto

Webserveis Points 7701

Update Reading this post A splashscreen efficient (in) I add the opaque

Define the background of the splashscreen in a drawable background_splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item android:drawable="@color/colorSplashScreen"/>
    <item android:top="-48dp">
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>
</layer-list>

I apply a sliding top to adjust well to the transition of the screen pre-load to the splashscreen

Assign as background to the drawable created in styles.xml

<style name="AppTheme.SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowEnterAnimation">@android:anim/fade_in</item>
    <item name="android:windowBackground">@drawable/background_splash</item>
</style>

Step I remove the idea of having text that appears below the icon of the app, because not found how to do it directly as a drawable.

layout splash_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@color/colorSplashScreen">

    <LinearLayout
        android:id="@+id/splashscreen"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical|center_horizontal">

        <ImageView
        android:layout_width="wrap_content"
        android:contentDescription="@string/about.alt_logo"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center"/>

</LinearLayout>



Optional code to change the theme on the fly, in case you do not make a splascreen with an activity intermediate:

public class MyMainActivity extends AppCompatActivity {
 @Override
  protected void onCreate(Bundle savedInstanceState) {

    setTheme(R.style.Theme_MyApp); //Tu Tema general
    super.onCreate(savedInstanceState);
    // ...
  }
}

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X