Friday, 14 February 2014

Splash Screen


Android splash screen are normally used to show  some kind of progress before the application loads completely.That progress can be getting data from Sqlite database, data from internet or it can be any background process. We can use splash screen for just showing our Application logo

splashscreen.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
  // here background image should be kept under res/drawable folder
    android:background="@drawable/bishop"
    tools:context=".SplashscreenActivity" >

    <TextView
        android:id="@+id/splashtextview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="40dp"
        android:layout_marginLeft="16dp"
        android:text="Bishop Heber"
        android:textColor="#FFFAFA"
        android:textSize="40dp" />
</RelativeLayout>

Splash Screen Activity:

package com.solomon.splashscreenexample;

import android.R.color;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.view.Menu;
import android.widget.TextView;

public class SplashscreenActivity extends Activity {
//In AndroidMainfest.xml this file should be declared as launcher Activity
//Splashscreen timer is for showing splashscreen for particular time.
private static int SPLASH_TIME_OUT=3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
TextView splashtxt = (TextView)findViewById(R.id.splashtextview);
//Getting ttf(True type font) font style.
//Before we have to copy ttf file in assets folder
Typeface type = Typeface.createFromAsset(getAssets(), "epohistory.ttf");
// setting style to textview
splashtxt.setTypeface(type);
  new Handler().postDelayed(new Runnable(){
     /* Inside this anonymous inner class we are creating a runnable thread and 
      * overriding run method. This anonymous inner class is subclass to Handler class                               */

@Override
public void run() {
/* Creating an Intent to start another activity
 * After starting MainpageActivity  i am closing this (SplashscreenActivity) 
        * Activity.
         */
Intent i = new Intent(SplashscreenActivity.this,MainpageActivity.class);
startActivity(i);
finish();
}
      },SPLASH_TIME_OUT);
   }
}

Main Page Activity:


package com.solomon.splashscreenexample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class MainpageActivity extends Activity{
// In AndroidMainfest.xml file we should declare this Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Creating an linearlayout and setting its orientation as Vertical
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
//Creating an Textview 
TextView mainpagetxt = new TextView(this);
mainpagetxt.setText("Main page TextView");
mainpagetxt.setGravity(Gravity.CENTER);
//setting textview to Linearlayout
ll.addView(mainpagetxt);
this.setContentView(ll);
}

}

AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.solomon.splashscreenexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.solomon.splashscreenexample.SplashscreenActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
  // Here we are declaring MainpageActivity       
   <activity
            android:name="com.solomon.splashscreenexample.MainpageActivity">
          
        </activity>
    </application>
</manifest>

Output:

     This screen will appear up to 3 seconds after it will close and another Activity will open




No comments:

Post a Comment