Simple splash screen

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Simple splash screen

Postby a.vogel » Fri Feb 08, 2008 11:09 am

[font=Arial Black] A simple splash screen[/font]

Alternative which automatically starts another Activity :arrow: :src: here.


What you learn: You will learn how to create a simple splashscreen for your application.

Difficulty: 0.5 of 5 :)

:idea: Questions/Problems: Simply post below...

Description:
This is a implementation of a simple splash screen. It will show a image on start and removes it after a specific time:

1. Define you splash screen in your layout file:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         <ImageView id="@+id/splashscreen" android:layout_width="wrap_content"
  6.                 android:layout_height="fill_parent" android:src="@drawable/splash"
  7.                 android:layout_gravity="center"/>
  8.         <TextView android:layout_width="fill_parent"
  9.                 android:layout_height="wrap_content" android:text="Hello World, splash"/>
  10. </LinearLayout>
  11.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


2. Add a handler to your start activity:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private Handler splashHandler = new Handler() {
  2.                 @Override
  3.                 public void handleMessage(Message msg) {
  4.                         switch (msg.what) {
  5.                         case STOPSPLASH:
  6.                                 //remove SplashScreen from view
  7.                                 splash.setVisibility(View.GONE);
  8.                                 break;
  9.                         }
  10.                         super.handleMessage(msg);
  11.                 }
  12.         };
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


3. In onCreate create a new message and send it delayed to the handler:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Message msg = new Message();
  2. msg.what = STOPSPLASH;
  3. splashHandler.sendMessageDelayed(msg, SPLASHTIME);
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Here is the complete code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.test.splash;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.view.View;
  8. import android.widget.ImageView;
  9.  
  10. public class splash extends Activity {
  11.        
  12.         private static final int STOPSPLASH = 0;
  13.         //time in milliseconds
  14.         private static final long SPLASHTIME = 3000;
  15.        
  16.         private ImageView splash;
  17.        
  18.         //handler for splash screen
  19.         private Handler splashHandler = new Handler() {
  20.                 /* (non-Javadoc)
  21.                  * @see android.os.Handler#handleMessage(android.os.Message)
  22.                  */
  23.                 @Override
  24.                 public void handleMessage(Message msg) {
  25.                         switch (msg.what) {
  26.                         case STOPSPLASH:
  27.                                 //remove SplashScreen from view
  28.                                 splash.setVisibility(View.GONE);
  29.                                 break;
  30.                         }
  31.                         super.handleMessage(msg);
  32.                 }
  33.         };
  34.        
  35.     /** Called when the activity is first created. */
  36.     @Override
  37.     public void onCreate(Bundle icicle) {
  38.         super.onCreate(icicle);
  39.         setContentView(R.layout.main);
  40.                         splash = (ImageView) findViewById(R.id.splashscreen);
  41.                         Message msg = new Message();
  42.                         msg.what = STOPSPLASH;
  43.                         splashHandler.sendMessageDelayed(msg, SPLASHTIME);
  44.     }
  45. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Please visit: www.my-travelload.de
User avatar
a.vogel
Freshman
Freshman
 
Posts: 4
Joined: Fri Feb 08, 2008 10:53 am
Location: Jena, Germany

Top

Postby plusminus » Fri Feb 08, 2008 3:24 pm

Hello a.vogel,

thanks for sharing your snippet. :!: (Added some heading-stuff)
I added my own implementation of a SplashScreen :src: here and crosslinked both, this and my tutorial.

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Re: Simple splash screen

Postby drhu00 » Wed Feb 13, 2008 3:26 pm

Can the splash screen contains also some text as well as an image?
drhu00
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue Feb 12, 2008 6:41 pm

Re: Simple splash screen

Postby plusminus » Wed Feb 13, 2008 3:35 pm

Hello drhu00,

drhu00 wrote:Can the splash screen contains also some text as well as an image?

It already doescontain an ImageView and a TextView.
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.      <ImageView id="@+id/splashscreen" android:layout_width="wrap_content"
  2.          android:layout_height="fill_parent" android:src="@drawable/splash"
  3.          android:layout_gravity="center"/>
  4.      <TextView android:layout_width="fill_parent"
  5.          android:layout_height="wrap_content" android:text="Hello World, splash"/>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Re: Simple splash screen

Postby drhu00 » Wed Feb 13, 2008 3:51 pm

I know there is textview in the xml file, but the question is that "Hello World, Splash" only show up after 3 seconds. In the splash screen, only the image show up. What I like is the splash window contains an image in the center and on the top line and bottom line there are some string. See below
***************
Splash Title

<Splash Image>

by drhu00
***************
drhu00
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue Feb 12, 2008 6:41 pm

Postby plusminus » Wed Feb 13, 2008 4:04 pm

Hello

:oops: my bad.

a.vogel makes the View invisible inside the Handler (after 3 seconds), you can add as many views as you want (or combine them within one XYZLayout).
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                     //...
  2.                     //remove SplashScreen from view
  3.                     splash.setVisibility(View.GONE);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


In your case it will probably be easier to choose the alternative :darrow:, because there you can simply use another layout.xml-file and there will be no need to set views to invisible.
Btw: Alternative which automatically starts another Activity :arrow: :src: here.


Regards,
plusminus
Last edited by plusminus on Wed Feb 13, 2008 4:08 pm, edited 1 time in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby a.vogel » Wed Feb 13, 2008 4:04 pm

For showing more than one item on the splash screen, put all items in a LinearLayout (or something similar) and call setVisibility for this container item.
Please visit: www.my-travelload.de
User avatar
a.vogel
Freshman
Freshman
 
Posts: 4
Joined: Fri Feb 08, 2008 10:53 am
Location: Jena, Germany

Re: Simple splash screen

Postby drhu00 » Wed Feb 13, 2008 8:01 pm

I tried the alternative, but it doesn't work.
drhu00
Junior Developer
Junior Developer
 
Posts: 24
Joined: Tue Feb 12, 2008 6:41 pm

Postby a.vogel » Thu Feb 14, 2008 8:40 am

A sample xml file containing more than one item looks like:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.         android:orientation="vertical" android:layout_width="fill_parent"
  3.         android:layout_height="fill_parent">
  4.         <LinearLayout id="@+id/splashscreen" android:orientation="vertical"
  5.                 android:layout_width="fill_parent" android:layout_height="fill_parent">
  6.                 <ImageView android:layout_width="wrap_content"
  7.                         android:layout_height="wrap_content" android:src="@drawable/splash"
  8.                         android:layout_gravity="center"/>
  9.                 <TextView android:layout_width="fill_parent"
  10.                         android:layout_height="wrap_content"
  11.                         android:text="This is a splash!!"/>
  12.         </LinearLayout>
  13.         <TextView android:layout_width="fill_parent"
  14.                 android:layout_height="wrap_content" android:text="Hello World, splash"/>
  15. </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Now you have to replace the existing ImageView in the java code with LinearLayout and it should work.

PS: In the tutorial code above the height of the ImageView is set to fill_parent. Replace it with wrap_content, otherwise it will cover the other items in this view.
Please visit: www.my-travelload.de
User avatar
a.vogel
Freshman
Freshman
 
Posts: 4
Joined: Fri Feb 08, 2008 10:53 am
Location: Jena, Germany

error in java file

Postby neel » Mon Feb 18, 2008 8:02 pm

hi
this is my java file

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.neel;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.view.View;
  8. import android.widget.ImageView;
  9.  
  10. public class NewSplash extends Activity {
  11.      
  12.      private static final int STOPSPLASH = 0;
  13.      //time in milliseconds
  14.      private static final long SPLASHTIME = 3000;
  15.      
  16.      private ImageView splash;
  17.      
  18.      //handler for splash screen
  19.      private Handler splashHandler = new Handler() {
  20.           /* (non-Javadoc)
  21.            * @see android.os.Handler#handleMessage(android.os.Message)
  22.            */
  23.           @Override
  24.           public void handleMessage(Message msg) {
  25.                switch (msg.what) {
  26.                case STOPSPLASH:
  27.                     //remove SplashScreen from view
  28.                     splash.setVisibility(View.GONE);
  29.                     break;
  30.                }
  31.                super.handleMessage(msg);
  32.           }
  33.      };
  34.      
  35.     /** Called when the activity is first created. */
  36.     @Override
  37.     public void onCreate(Bundle icicle) {
  38.         super.onCreate(icicle);
  39.         setContentView(R.layout.main);
  40.                <span style="color: darkred">splash = (ImageView) findViewById(R.id.splashscreen);</span>
  41.                Message msg = new Message();
  42.                msg.what = STOPSPLASH;
  43.                splashHandler.sendMessageDelayed(msg, SPLASHTIME);
  44.     }
  45. }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4


and this is my main.xml file

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical" android:layout_width="fill_parent"
  4.     android:layout_height="fill_parent">
  5.      <ImageView id="@+id/splashscreen" android:layout_width="wrap_content"
  6.          android:layout_height="fill_parent" android:src="@drawable/weatherforecast"
  7.          android:layout_gravity="center"/>
  8.      <TextView android:layout_width="fill_parent"
  9.          android:layout_height="wrap_content" android:text="Hello World, splash"/>
  10. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


i get an error in this line saying "R.id cannot be resolved"
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. splash = (ImageView) findViewById(R.id.splashscreen)
Parsed in 0.035 seconds, using GeSHi 1.0.8.4
neel
Developer
Developer
 
Posts: 31
Joined: Fri Feb 15, 2008 5:09 am
Location: San Jose

Postby a.vogel » Tue Feb 19, 2008 8:25 am

Which version of the SDK do you use. For version "m3-rc37a" the code should be ok. But for the new version of the SDK ("m5-rc14") you should change the id-tag in xml file to:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. android:id="@+id/splashscreen"
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
Please visit: www.my-travelload.de
User avatar
a.vogel
Freshman
Freshman
 
Posts: 4
Joined: Fri Feb 08, 2008 10:53 am
Location: Jena, Germany

splash screen error

Postby neel » Sun Feb 24, 2008 10:53 pm

I get teh following error in the console of eclipse


Code: Select all
[2008-02-24 13:49:25 - Splash] ActivityManager: Error type 3
[2008-02-24 13:49:25 - Splash] ActivityManager: Error: Activity class {com.neel/com.neel.Splash} does not exist.
neel
Developer
Developer
 
Posts: 31
Joined: Fri Feb 15, 2008 5:09 am
Location: San Jose

splash screen all visible

Postby vdoppia » Wed Apr 22, 2009 6:10 pm

Hi, i'm new in this comunity... i have use this code to make a splash screen. There is a metod to see the splash screen all time I open my application? Now I see splash screen only the first time I install my application and start it (in development emulator).
vdoppia
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Apr 22, 2009 5:49 pm

Crashing issues?

Postby ashywood » Tue Apr 28, 2009 2:53 am

Hey All,

I know this is an old thread, but I was hoping to get feedback.

I'm currently just trying to just add a splash screen before my test app launches "Hello, World." The result is either my emulator hangs or it crashes. The error I'm getting stems from the line that's bold. Is there a reason the splash screen won't finish its' time cycle and continue on to 'Hello, World?' Thanks for any help!



@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case STOPSPLASH:
//remove SplashScreen from view
splash.setVisibility(View.GONE);

[void android.view.View.setVisibility(int visibility)
Note: The Javadoc for this element could neither be found in the attached source nor the attached Javadoc.

]
break;
}
super.handleMessage(msg);
}
};
ashywood
Freshman
Freshman
 
Posts: 5
Joined: Tue Apr 28, 2009 2:34 am

Postby jpe30 » Mon Jun 08, 2009 11:28 pm

I'm having the same problem as ashy, the setVisibility(View.GONE) line throws a null pointer exception. I need this as I need to show 2 splash screens, one right after the other.

Any suggestions as to how to get this working?
jpe30
Freshman
Freshman
 
Posts: 4
Joined: Mon Jun 08, 2009 11:24 pm

Top
Next

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests