3

I'm pretty new to Java and xml i'm looking to have a splash screen run when i start up my app for about 5 seconds. I've taken code for the splash screen from stack overflow to set it up but i cant get it to run for some reason could anybody help me out! Cheers

MY splash class

   package com.darraghoflaherty.competer.game;

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

public class Splash extends Activity {

/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 5000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.splashscreen);

    /* New Handler to start the Menu-Activity
     * and close this Splash-Screen after some seconds.*/
    new Handler().postDelayed(new Runnable(){
        @Override
        public void run() {
            /* Create an Intent that will start the Menu-Activity. */
            Intent mainIntent = new Intent(Splash.this,Menu.class);
            Splash.this.startActivity(mainIntent);
            Splash.this.finish();
        }
    }, SPLASH_DISPLAY_LENGTH);
}

My xml code

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#0099FF">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="@string/ss1"
    android:id="@+id/ss1"
    android:textColor="#ffffff"
    android:textSize="260sp"/>




 </RelativeLayout>
Darragh O'Flaherty
  • 995
  • 3
  • 13
  • 30

5 Answers5

2

first change this peace of code :

public void run() {
    /* Create an Intent that will start the Menu-Activity. */
    Intent mainIntent = new Intent(Splash.this,Menu.class);
    Splash.this.startActivity(mainIntent);
    Splash.this.finish();
}

To

public void run() {
    /* Create an Intent that will start the Menu-Activity. */
    Intent mainIntent = new Intent(getApplicationContext(),Menu.class);
    startActivity(mainIntent);
    finish();
}

Now your code is clear, the error must come from the manifest file. Go to the manifest file and change the position of the intent-filter from the Mainactivity to the slashscreen activity. here the code :

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
B.Moataz
  • 97
  • 4
1

My guess is that you have not changed the launcher activity in the manifest. Android looks in the AndroidManifest.xml to select the activity to start first. Your manifest probably contains these lines:

<activity android:name=".Menu" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This should be changed to:

<activity android:name=".Splash" android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
<activity android:name=".Menu"/>

It is also a good convention to name activities XyzActivity, so in your case MenuActivity and SplashActivity.

Daniel Zolnai
  • 16,487
  • 7
  • 59
  • 71
1
/* New Handler to start the Menu-Activity
 * and close this Splash-Screen after some seconds.*/

        Thread timer = new Thread(){
        public void run(){
            try{
                Thread.sleep(2000);
            }
            catch(InterruptedException e){
                e.printStackTrace();
            }
            finally{
                 Intent mainIntent = new Intent(Splash.this,Menu.class);

                startActivity(mainIntent );

                finish();
            }
        }

    };// end thread
    timer.start();
Golnaz
  • 19
  • 5
0

use this code for the splash screen ...

public class SPLASH extends Activity {


protected boolean _active = true;
protected int _splashTime = 3000; // time to display the splash screen in MICROSECONDS 


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



    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while (_active && (waited < _splashTime)) {
                    sleep(100);
                    if (_active) {
                        waited += 100;
                    }
                }
            } catch (Exception e) {

            } finally {

                startActivity(new Intent(Splash.this,Menu.class));
                finish();
            }
        };
             };
    splashTread.start();

    }

}

and make the Splash activity a launcher activity by using

<activity
        android:name=".SPLASH"
        android:label="@string/app_name"
        android:theme="@style/NoActionBar" 
        android:screenOrientation="portrait">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Hope this will help u

Saikiran Sondarkar
  • 344
  • 1
  • 3
  • 9
-1

Hey checkOut it i have improve little bit. thanks

public class Splash extends Activity {

private final int SPLASH_DISPLAY_LENGTH = 5000;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.splashscreen);

   nav();
}

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


         Intent mainIntent = new Intent(Splash.this,Menu.class);
        startActivity(mainIntent);
       finish();

        }
    }, SPLASH_DISPLAY_LENGTH);
  }

@Override
protected void onPause() {
    super.onPause();
    finish();
}

}

Saveen
  • 4,120
  • 14
  • 38
  • 41
  • 2
    Would you like to explain what have you improved? – user3141985 Nov 16 '14 at 18:31
  • Hey first thing we can not use like activity name .startactivity and second thing you can finish activity in onPause :) – Saveen Nov 17 '14 at 00:19
  • what the fish...-1 for the authenticity with which you told 'can not use like activity name.startactivity'. Splash.this.startActivity() will work perfectly fine as startActivity() He can! but not necessary! – Rahul Jan 29 '16 at 13:52