Thursday, February 21, 2019

Splash screen when open app

We want to show a splash screen when open app.
In folder layout, create a file name spla.xml, set an imageView inside, for example, a brand image.


Create a class name splash, setContentView to spla.xml.
public class splash extends Activity {  
      private static int SPLASH_TIME_OUT = 2000;
      
         @Override
     protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.spla);    
         
           new Handler().postDelayed(new Runnable() {  
                @Override
         public void run() {
                    
                     // chạy activity chính khi hết 2 giây
        Intent i = new Intent(splash.this, MainActivity.class);
               startActivity(i);        
              finish();
                 }
             }, SPLASH_TIME_OUT);
         }
     }
Change value SPLASH_TIME_OUT = 2000; to show image longer or shorter.
Last, go to AndroidManifest.xml set class splash to first class, declare class MainActivity to below it.

Now we will see splash image show in 2 seconds when open app.

No comments:

Post a Comment