It is best way to represent your logo or show start up image until all data are not loaded to the application or first page.
To understand the splash screen you must have a knowledge of Thread in java, because splash scree is appear for only some milliseconds (3000 i.e 3 secs) which means running thread for some time and after that we have to terminate it.
Now lets start the implementation of splash screen.
create new layout file and copy following code inside it.
activity_splash_screen.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/appsplash"
android:orientation="vertical" >
</LinearLayout>
in above i have add image in background of LinearLayout. So replace it with your image.
Add new class file for splash sceeen.
SplashScreen.java
package com.ilearn.xtreme;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
// Splash screen timer in milliseconds
private static int SPLASH_TIME_OUT = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_scren);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over and name of target activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
Dont forget to make this activity as launcher activity in manifest file by adding following code.
<activity
.........some code
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
.........some code
</activity>
share it and enjoy.
comment wherever you stuck thank you :)
No comments:
Post a Comment