Start app with splash screen image

Asked

Viewed 1,027 times

5

I want when my application was started, an image would appear and then open the application itself. Similar to the Youtube app that when starting, a picture of the Youtube symbol appears and then opens the app.

  • Search on Splash screen Voce will find what you need.

2 answers

7

This resource you seek is called SplashScreen.

First, create a layout with the image you’d like to appear at the beginning. (Remember that it should be in the Drawable folder)

You’ll have something like:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:orientation="vertical" >  

   <ImageView  
     android:id="@+id/imageView1"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:src="@drawable/imagem_splash" />  

 </LinearLayout> 

Now, you must create a class so that we can define how long this layout will run.

Note in the example below that we have defined a int as long as the screen will be open. After that, we have the method run where we define what Activity will be called after the end of the time you defined earlier.

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

 public class SplashScreen extends Activity {  

   //Define tempo que a tela vai exibir. (tempo em milisegundos)
   private static int SPLASH_TIME_OUT = 3000;  

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

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

       @Override  
       public void run() {  
            //Método que será executado uma vez.. Na abertura do app.  
            Intent i = new Intent(SplashScreen.this, MainActivity.class);  
         startActivity(i);  

         finish();  
       }  
     }, SPLASH_TIME_OUT);  
   }  
 }  

Finally, we must not forget to go AndroidManifest.xml and create our new Activity (Splash) there. You’ll have something like:

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

0

// time long private splashRetraso = 2000;

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

//code that does what you want

    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            Intent NovoLaytout;
            NovoLaytout = new Intent(Tlsplah.this,MainActivity.class);
            startActivity(NovoLaytout);

            Tlsplah.this.finish();


        }
    };

    Timer timer = new Timer();
    timer.schedule(task,splashRetraso);

}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.