Thursday, 11 June 2015

Splash Screen in android

No comments
In this android example we show you how to create Splash screen in android. the Splash screen is like a into page. splash screen is an activity that will show when your app is starting for a defined time duration e.g.5 second, after this redirect to main screen of app.

Lets go for fun with code:

CREATE NEW ANDROID PROJECT

In Splash screen example we need:

1) firstscreen.xml
2) splash_screen.xml

3) FirstScreen.java
4) SplashScreen.java

Step 1: Write code into firstscreen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#26A69A"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="This is a (firstscreen.xml)"
        android:textAppearance="?android:attr/textAppearanceLarge" />


</LinearLayout>

Step 2: Write code into splash_screen.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#26A69A"
    android:orientation="vertical" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="100dp"
        android:scaleType="center"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Androidapplink"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textSize="30sp" />


</LinearLayout>

Step 3: Write code into FirstScreen.java

package dev.androidapplink.splashscreenapp;

import dev.androidapplink.splashscreenapp.R;

import android.app.Activity;
import android.os.Bundle;

public class FirstScreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstscreen);

}

@Override
protected void onDestroy() {

super.onDestroy();

}

}


Step 4: Write code into SplashScreen.java

package dev.androidapplink.splashscreenapp;

import dev.androidapplink.splashscreenapp.R;

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

public class SplashScreen extends Activity {

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

// METHOD 1 Using Thread timing

//Create Thread that will sleep for 5 seconds 
Thread background = new Thread() {
public void run() {

try {
// Thread will sleep for 5 seconds
sleep(5 * 1000);

// After 5 seconds redirect to another intent
Intent i = new Intent(getBaseContext(), FirstScreen.class);
startActivity(i);

// Remove activity
finish();

} catch (Exception e) {

}
}
};

// start thread
background.start();

// METHOD 2 Using Handler time delay

/*
* new Handler().postDelayed(new Runnable() {
* // Using handler with postDelayed called runnable run method
* @Override public void run() { Intent i = new
* Intent(SplashScreen.this, FirstScreen.class); startActivity(i);
* // close this activity finish(); } }, 5*1000); // wait for 5 seconds
*/
}

@Override
protected void onDestroy() {

super.onDestroy();

}
}

NOTE: No need any permission in AndroidManifest.xml

Step 5: Run Your Project:





No comments :

Post a Comment

Follow me Share