10
votes

I'm brand new to Cordova and am trying to get the splash screen working on Android. I've followed a few tutorials to the letter and have reviewed the questions here on SO but it's not working yet.

I'm using the 'Cordova CLI' approach - here is my /config.xml:

   <?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.hello" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
    <name>HelloWorld</name>

    <preference name="SplashScreen" value="screen" />       
    <preference name="SplashScreenDelay" value="2000" />    

    <description>
        A sample Apache Cordova application that responds to the deviceready event.
    </description>
    <author email="[email protected]" href="http://cordova.io">
        Apache Cordova Team
    </author>
    <content src="index.html" />
    <plugin name="cordova-plugin-whitelist" spec="1" />
    <access origin="*" />
    <allow-intent href="http://*/*" />
    <allow-intent href="https://*/*" />
    <allow-intent href="tel:*" />
    <allow-intent href="sms:*" />
    <allow-intent href="mailto:*" />
    <allow-intent href="geo:*" />
    <platform name="android">

        <allow-intent href="market:*" />

        <splash src="res/screen/android/splash-logo.png" density="land-hdpi"/>
        <splash src="res/screen/android/splash-logo.png" density="land-ldpi"/>
        <splash src="res/screen/android/splash-logo.png" density="land-mdpi"/>
        <splash src="res/screen/android/splash-logo.png" density="land-xhdpi"/>

        <splash src="res/screen/android/splash-logo.png" density="port-hdpi"/>
        <splash src="res/screen/android/splash-logo.png" density="port-ldpi"/>
        <splash src="res/screen/android/splash-logo.png" density="port-mdpi"/>
        <splash src="res/screen/android/splash-logo.png" density="port-xhdpi"/>     

        <icon density="ldpi" src="res/screen/android/icon.png" />
        <icon density="mdpi" src="res/screen/android/icon.png" />
        <icon density="hdpi" src="res/screen/android/icon.png" />
        <icon density="xhdpi" src="res/screen/android/icon.png" />      
    </platform>

    <platform name="ios">
        <allow-intent href="itms:*" />
        <allow-intent href="itms-apps:*" />
    </platform>
</widget>

I know I should have different image sizes for each density but this is a test project so please ignore that.

I've added the cordova-plugin-splashscreen to my project.

  cordova plugin add cordova-plugin-splashscreen 

The splashscreen is still not being displayed - instead a blackscreen turns up when the app runs on the emulator. After a couple of seconds, the blackscreen goes away and the default 'device-ready' Cordova screen appears.

I've done everything suggested in other places - can you guys spot what's wrong?

FYI I was looking at the /plataforms directory and it seems things are correctly set.

The snippet below is from /platforms/android/res/xml/config.xml:

<splash density="land-hdpi" src="res/drawable-land-xhdpi/screen.png" />
<splash density="land-ldpi" src="res/drawable-land-xhdpi/screen.png" />
<splash density="land-mdpi" src="res/drawable-land-xhdpi/screen.png" />
<splash density="land-xhdpi" src="res/drawable-land-xhdpi/screen.png" />
<splash density="port-hdpi" src="res/drawable-land-xhdpi/screen.png" />
<splash density="port-ldpi" src="res/drawable-land-xhdpi/screen.png" />
<splash density="port-mdpi" src="res/drawable-land-xhdpi/screen.png" />
<splash density="port-xhdpi" src="res/drawable-land-xhdpi/screen.png" />
<icon density="ldpi" src="res/screen/android/icon.png" />
<icon density="mdpi" src="res/screen/android/icon.png" />
<icon density="hdpi" src="res/screen/android/icon.png" />
<icon density="xhdpi" src="res/screen/android/icon.png" />

There are screen.png files under every directory listed above - so it seems Cordova is doing its work correctly.


Main activity:

package com.example.hello;

import android.os.Bundle;
import org.apache.cordova.*;

public class MainActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
    }
}
2
could you post main activity code ?Jay Rathod RJ
Errr.... maybe. Where can I find it? :) I thought the activity stuff was Android-specific and as I'm using Cordova CLI I wasn't supposed to meddle with it.user1348997
in your android project src folder main activity java file will be their.Jay Rathod RJ
your main activity looking perfect but have you added splash screen plugins properly ?Jay Rathod RJ
Which plugin version are you using? The version can be seen in cordova plugins output. Try to cordova plugin rm splashscreen && cordova plugin add https://github.com/apache/cordova-plugin-splashscreen as @jaydroider suggested. Alternatively try to add <preference name="FadeSplashScreenDuration" value="500"/> to config.xml.daserge

2 Answers

3
votes

I've struggled with this issue for quite some time too, and found out that the problem is in the values for SplashScreenDelay and FadeSplashScreenDuration.

If you look at SplashScreen.java, you'll find that the showSplashScreen function executes the following:

final int fadeSplashScreenDuration = getFadeDuration();
final int effectiveSplashDuration = Math.max(0, splashscreenTime - fadeSplashScreenDuration);

In your config, you've set the SplashScreenDelay to be 2000 ms.

getFadeDuration looks for the FadeSplashScreenDuration element and when not found (in this case) falls back to the default DEFAULT_SPLASHSCREEN_DURATION, which is 3000 ms, resulting in a negative value for the effectiveSplashDuration. Apparently this will lead to no splashscreen being displayed.

So the solutions are

  • add a FadeSplashScreenDuration element to the configuration with the value set to 0. (or any number smaller than 2000)
  • Increase the value of SplashScreenDelay to 4000 (or any number larger than 3000)
-1
votes

Try to use this in your cofig.xml file.

<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />

 <feature name="SplashScreen" >
    <param
        name="android-package"
        value="org.apache.cordova.splashscreen.SplashScreen" />
    <param
        name="onload"
        value="true" />
</feature>

this might solve your problem.