Skip to content Skip to sidebar Skip to footer

Showing Splash Screen In Android Apps

Have you ever seen the page in many apps that before launching the first activity appears and somehow it is like a waiting page ?! I am beginner to android and when I click on my a

Solution 1:

Technically thing you are talking about is called splash screen.

Splash screen is used to show cold start where we can make our things ready to run an application. Google advocated to use it.

There are few approaches to show splash screen.

  1. You can use CountDownTimer like this. In SplashActivity.java

    privateintTIME_OUT=3000;
    
    CountDownTimercountDownTimer=newCountDownTimer(TIME_OUT, TIME_OUT) {
    
    @OverridepublicvoidonTick(long millisUntilFinished)
    {
        // Leave this
    }
    
    @OverridepublicvoidonFinish()
    {
        // Start your app main activityIntenti=newIntent(SplashScreen.this, MainActivity.class);
        startActivity(i);
    
        // close this activity
        finish();
    }
    Copy
    
    }.start();
  2. You can use Handler like this. In SplashActivity.java

    privatestaticint TIME_OUT = 3000;
    
    new Handler().postDelayed(new Runnable() 
    {
        @Override
        publicvoidrun() {
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);
    
                finish();
            }
    

    }, TIME_OUT);

  3. Using theme - correct way of doing

Add background_splash.xml file in drawable folder

<layer-listxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@color/gray"/><item><bitmapandroid:gravity="center"android:src="@mipmap/ic_launcher"/></item></layer-list>

Add these lines in styles.xml

<stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar"><!-- Customize your theme here. --></style><stylename="SplashTheme"parent="Theme.AppCompat.NoActionBar"><itemname="android:windowBackground">@drawable/background_splash</item></style>

Add these lines in AndroidManifest.xml

<activityandroid:name=".SplashActivity"android:theme="@style/SplashTheme"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity>

Finally add an activity called SplashActivity.java and add these code

publicclassSplashActivityextendsAppCompatActivity 
{
    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        Intentintent=newIntent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}

Solution 2:

You can use the Splash Screen for this Working code:

publicclassMainsplashextendsActivity {


     /** Duration of wait **/privatefinalintSPLASH_DISPLAY_LENGTH=3000;

    /** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle icicle) {
        super.onCreate(icicle);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splashscreen);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);




        /* New Handler to start the Menu-Activity 
         * and close this Splash-Screen after some seconds.*/newHandler().postDelayed(newRunnable(){
            @Overridepublicvoidrun() {
                /* Create an Intent that will start the Menu-Activity. */IntentmainIntent=newIntent(Mainsplash.this,MainActivity.class);
                Mainsplash.this.startActivity(mainIntent);
                Mainsplash.this.finish();
            }
        }, SPLASH_DISPLAY_LENGTH);
    }

}

Xml code:

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/load_src"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/splashimg"android:gravity="center"android:orientation="vertical" ><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><LinearLayoutandroid:id="@+id/ll_v1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_alignParentBottom="true"
         ><ProgressBarandroid:id="@+id/prg"android:layout_width="wrap_content"android:layout_height="wrap_content" /></LinearLayout></RelativeLayout></LinearLayout>

And also in Manifest file

<activityandroid:name=".Mainsplash"android:label="@string/app_name"android:screenOrientation="portrait" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity>

use the intent filter for that splash activity to make it launch first

hope this helps

Post a Comment for "Showing Splash Screen In Android Apps"