Android splash screen is one of the best way to make your android app look more attractive and also a way to advertise your brand in your app. We also use splash screen to load some data from service (http) or run some other process in background or preparing data to show in next activity. In this post we are going to make a splash screen and i also load website in application using webview.

 

Android Splash screen

1. Open your eclipse and create new Android application project { File ⇒ New Android ⇒ Application Project } name your project select icon if you want to change and done.

2. To run splash we create separate class in our package (Splash.java) and also create new activity in layout (splashscreen.xml).

3. Now open activity file (splashscreen.xml) inside layout folder in project and design it and save

splashscreen.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#d73d32" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="126dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/trinitylogo" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignRight="@+id/imageView1"
        android:layout_marginBottom="45dp"
        android:src="@drawable/by" />

</RelativeLayout>

Now for creating splash i am using relative layout and add two image and also add background color in my layout. You can design whatever you want to show in your splash.

 Splash.java

package com.splash;

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

public class Splash extends Activity {
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splashscreen);
        
        final Handler handel = new Handler();
        handel.postDelayed(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Intent loadSplash = new Intent(Splash.this, MainActivity.class);
				
				startActivity(loadSplash);
				
				finish();
			}
		}, 5000);
    }
}

In my splash.java i call new handler class and use postDelayed() method to display our splash for some interval of time and when time is out we load our main activity as shown in above code.

Now open your AndroidManifest.xml and register your splash screen activity as first activity

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

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/anehtrinity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <activity
            android:name="com.splash.Splash"
            android:label="@string/app_name"
            android:theme="@style/Theme.MyAppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.splash.MainActivity" 
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
        </activity>
    </application>

</manifest>

Now you can launch your application in your device or emulator.

Note when you launch your application a for first few second a black screen was appear if your splash screen background have black color its ok but if your theme have some other background you need to create a custom theme to solve that problem.

Code to create a custom theme for splash screen

Now create a new xml file inside values folder and paste use these code

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Base application theme is the default theme. -->
<style name="Theme" parent="android:style/Theme" />

<!-- Theme for splash screen-->
<style name="Theme.MyAppTheme" parent="Theme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@color/red</item>

</style>
</resources>

you can define your own style as i create in above xml file and save it now you can call this theme in where you add splash screen activity in AndroidManifest.xml file

<activity
            android:name="com.splash.Splash"
            android:label="@string/app_name"
            android:theme="@style/Theme.MyAppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

And after doing this your problem is solve.

 Working on WebView in Android

WebView is used to display a webpage, maps etc.. before using web view you need to add Internet permission in your AndroidManifest.xml file

<uses-permission android:name="android.permission.INTERNET"/>

before application

MainActivity

This is my main activity class where i load my webview.

package com.splash;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.Menu;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {

	private WebView webView;
	private ProgressDialog progressBar;
	private static final String TAG = "Main";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		webView = (WebView) findViewById(R.id.trnity);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setLoadWithOverviewMode(true);
		webView.getSettings().setUseWideViewPort(true);
		webView.getSettings().setBuiltInZoomControls(true);
		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		progressBar = new ProgressDialog(MainActivity.this);
		progressBar.setMessage("Trinity tuts loading...");
		progressBar.setCancelable(false);
		progressBar.setCanceledOnTouchOutside(false);
		progressBar.show();
		webView.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Log.i(TAG, "Loading");
				view.loadUrl(url);
				return true;
			}

			public void onPageFinished(WebView view, String url) {
				Log.i(TAG, "Done loading " + url);
				if (progressBar.isShowing()) {
					progressBar.dismiss();
				}
			}

			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				Log.e(TAG, "Error: " + description);
				Toast.makeText(getApplicationContext(),
						"Oh no! " + description, Toast.LENGTH_SHORT).show();
				alertDialog.setTitle("Error");
				alertDialog.setMessage(description);
				alertDialog.setCancelable(false);
				alertDialog.show();

			}
		});
		webView.loadUrl("http://trinitytuts.com/");

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

In n above code first i remove my default title bar from my mainactivity file using code

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

Note that you need to call requestWindowFeature() before you setContentView() to remove your title bar.

and next i initialize webview and call some default feature and load url from internet on this application.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@android:style/Theme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:layout_margin="0dp"
    android:padding="0dp" >

    <WebView
        android:id="@+id/trnity"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

In my mainactivity.xml i create a webview where i display my site on this application.

You can download this code and try this apk in your android device from link above.

Hope you like.

🙂