Skip to main content

Create Splash Screen or Welcome Screen in Android

 To create Splash Screen for your Android App

  1. Create Handler Class
  2. Use PostDelayed() function
  3. Use Runnable (Code) & Time Delay


Steps:

A) Add Empty Activity

  1. Go to java -> New -> Activity -> Empty Activity
  2. Enter Activity Name: SplashActivity
  3. Hit Finish Button
B) Design Splash Screen
  1. Open layout -> activity_splash.xml
  2. Cope the Background Image you want to use
  3. Paste at drawables folder
  4. Choose a file name for your image.
  5. File name must be [a-z] in lowercase 0-9 digits or underscore. Capital Letter not allowed.
  6. Now open activity_splash.xml file
  7. Enter android:background="@drawable/bg_splash_screen"
  8. You are done
Structure of activity_splash
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".SplashActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg_splash_screen"
/>


C) Add Code in SplashActivity.java file

Here we use a Handler Class
PostDelayed Function to delay the display of the screen by 2500 milli second
And Runnable Method to end with.

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {

}
},2500);

This is how SplashActivity.java file look like:-

package xx.xxx.xxx; (It should be the name you choose while creating your android project)

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
public class SplashActivity extends AppCompatActivity {

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

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {

}
},2500);
}
}

D) Load MainActivity after 2.5 Second

Use Intent like this

public void run() {
Intent intent = new Intent( SplashActivity.this,MainActivity.class);
startActivity(intent);
}

Finally our SplashActivity.java looks like this:-

public class SplashActivity extends AppCompatActivity {

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

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent( SplashActivity.this,MainActivity.class);
startActivity(intent);
}
},2500);
}
}

NOTE IF YOU GET RED COLOR IN Intent, hit Option + Enter button. This will import related class automatically.

Comments

Popular posts from this blog

How to enable pinch zoom on a web view loading a url with pdf(Android)?

How to enable pinch zoom on a web view loading a url with pdf(Android)? Ref: https://stackoverflow.com/questions/16630382/how-to-enable-pinch-zoom-on-a-web-view-loading-a-url-with-pdfandroid   WebView mWebView = (WebView) findViewById(R.id.webView);         mWebView.getSettings().setJavaScriptEnabled( true ); WebSettings webSettings = mWebView.getSettings(); webSettings.setBuiltInZoomControls( true ); webSettings.setSupportZoom( true );

[SOLVED] Is Android Studio supported on Mac OS 10.15 (Catalina)? Android Studio Not Working on MAC

 Technically Yes! But not that all versions work the same way. While I upgraded to Android Studio to Version 4.1.2. But it did not work in OS 10.15. I deleted my previous android installations following this guide , but still nothing seemed to work. The hell bent installer seemed to get stuck in the middle. I contacted few experts who also appear clueless. A lot of time in my developers' life, I prompt to self - "Read Sid, Read!'. This was no different this time. Instead of searching for help online / offline, I started reading the Android Studio Release Note for version 4.1.2 . It clearly says ,  System Requirements  For MAC:  Mac® OS X® 10.10 (Yosemite) or higher, up to 10.14 (macOS Mojave) So, it may not work with MAC OS Catalina anyway. And it did not work as in my case. Strangely, my previous Android Version 4.0 worked. It is bit tricky to find old releases. But here is where you can find old Android Studio Archives . I downloaded Android Version 4.0, and it wor...

How to Align Views at The Bottom of The Screen

This is h ow you can Align Views at The Bottom of The Screen You know, you have to go to layout_main.xml file (If this is the layout you want to tweak with). Here we add a LinearLayout Component. Then place a TextView Component within that liner layout. Most important note: Add android:gravity="bottom" in the LinearLayout Component Add  android :gravity ="center_horizontal" in the TextView Component You are done! Here goes your SAMPLE code. < LinearLayout android :layout_width ="match_parent" android :layout_height ="match_parent" android :gravity ="bottom" android :orientation ="horizontal" > < TextView android :id ="@+id/bottomText" android :layout_width ="match_parent" android :padding ="2dp" android :textSize ="18sp" android :layout_height ="wrap_content" android :background ="@color/cardview_...