Tuesday, 18 October 2016

Easy Splash Screen For your android Applicattion

Hi All,
Get Stuck in add Welcome screen to your Android application?Here I am going to show You How to create easy splash screen to your android app.
for that we simply need

A.IDE-Android Studio latest version with SDK.
B.Android Emulator or android Device.

Step By procedure to create splash screen

1.Start Android Studio IDE
2.Start New PROJECT name as Easy Splash screen
3.Choose an empty activity with name Main Activity
4.After Loading the project go to res-value-style and change the theme in to 

 style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"

5.Add a new activity name as Splashscreen.
6.Remove default marginof splashscreen.xml file.
7.Add image view to splashscreen.xml file.
8.How to add image in project? for that see my blog
8.Add source for image in spashscreen.xml file.

  android:src="@drawable/android_boot_image"

9.Initialize image view in splashscreen.java by
  imageView= (ImageView) findViewById(R.id.imageView);

10.Create new directory in res directory name as anim and create new animation source file in    anim directory.
11.Set a duration for splashscreen


   android:duration="4000"

12.Create Object of Animation class in splash screen 
     Animation animation= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation);

13.Create set animation listener to animation object.
14.Start MainActivity in onAnimationEnd using intent object.
15.Run app using Android adb manager by clicking Run Button

End


MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;


import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
    //Declare ImageView in main Acivity for Handle the splashscreen Image

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Initialize imageview for hadling in mainactivity        toolbar=(Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }
}

Splashscreen.java


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class Splashscreen extends AppCompatActivity {
    ImageView imageView;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        imageView= (ImageView) findViewById(R.id.imageView);
        Animation animation= AnimationUtils.loadAnimation(getApplicationContext(), R.anim.animation);
        imageView.setAnimation(animation);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override            public void onAnimationStart(Animation animation) {

            }

            @Override            public void onAnimationEnd(Animation animation) {
                startActivity(new Intent(getApplicationContext(), MainActivity.class));

            }

            @Override            public void onAnimationRepeat(Animation animation) {

            }
        });
    }
}

activity_main.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"
tools:context="com.xiditech.easysplashscreeen.MainActivity">
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"/>

    <TextView
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:text="Hello World!" 
     android:textSize="40dp"
     android:gravity="center"
     />
</RelativeLayout>

activity_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"
    tools:context="com.xiditech.easysplashscreeen.Welcome">

    <ImageView        
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:layout_centerHorizontal="true"
android:src="@drawable/android_boot_image"/>
</RelativeLayout>

animation.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

<rotate    android:duration="4000"></rotate>
</set>