0

Code For splash.xml :

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/notice2"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".SplashScreen">
</RelativeLayout>

Code For AndroidManifest.xml :

  <application

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity

        android:name="com.kksworld.noticeboard.SplashScreen"
        android:label="@string/title_activity_splash_screen"

        android:theme="@style/AppTheme.NoActionBar">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.kksworld.noticeboard.LoginActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    <activity
        android:name="com.kksworld.noticeboard.Registration"
        android:label="@string/title_activity_registration"
        android:theme="@style/AppTheme.NoActionBar" />

</application>

`I am trying to add splash screen in my project. But after running the app the screen stops for the time i have set the timer but exact splash screen is not coming. After that everything is coming. Please Help. Thanks.

enter image description here

Code for Splash Activity :

public class SplashScreen extends Activity {

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_splash_screen);

        new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen.this, LoginActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Kundan Kumar
  • 151
  • 1
  • 2
  • 12

3 Answers3

1

Try using the bellow code

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.androidhive.androidsplashscreentimer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <!-- Splash screen -->
        <activity
            android:name="info.androidhive.androidsplashscreentimer.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- Main activity -->
        <activity
            android:name="info.androidhive.androidsplashscreentimer.MainActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest> 

activity_splash.xml

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

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/wwe_logo" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:textSize="12dp"
        android:textColor="#454545"
        android:gravity="center_horizontal"
        android:layout_alignParentBottom="true"
        android:text="www.androidhive.info" />

</RelativeLayout>

SplashScreen.java

package info.androidhive.androidsplashscreentimer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

public class SplashScreen extends Activity {

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        new Handler().postDelayed(new Runnable() {

            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }

}
Sujithrao
  • 789
  • 3
  • 12
  • 27
1

Splash screens that delay the app should not be used as they interrupt the users in whatever they want to do with your app.

But: There is a way better approach and it is even easier to implement! Every app needs some time to start and while its starting, most apps only display a white background as this is the default. You can change that however by adding a new style to your themes.xml that sets the windowBackground to any drawable

<style name="YourApp.AppTheme.Splashscreen" parent="YourApplicationsTheme">
    <item name="android:windowBackground">@drawable/splashscreen</item>
</style>

You can define the drawable like that if you want a logo on a coloured background:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item android:drawable="@color/some_color"/>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/some_logo"/>
    </item>
</layer-list>

Just add this theme to your <application> in the AndroidManifest.xml and your splash screen is already up and working.

Finally, set your theme back to your normal theme after the startup using

getApplication().setTheme(R.style.YourApplicationsTheme);

to prevent the splash from appearing whenever you switch activities.

All done! A beautiful splash screen without delaying the user.

Android design guidelines are also suggesting that way and you can also see this for reference.

iridic
  • 393
  • 2
  • 9
0

Check your layout files. Make sure you are using the correct layout files. In the java code you have used content_splash_screen.xml, but, you have posted the code for splash.xml.

There are no errors in the code, your code should run fine.

Arshad Mehmood
  • 409
  • 3
  • 14