Wednesday 13 December 2017

Create Splash Screen With Animation in Android Studio

Hello myself Satish Sonwale an Android Application Developer. In this tutorial am showing you How to create Splash screen animation in android Studio


                                                                        


So Lets Create 

  • First we will Create new Project "Splash_Example"
  • After this we need to Design our UI elements first so following is my code I have designed for in splash.XML file

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/bg"    tools:context="com.example.shreeganesha.splash.Splash">
    <ImageView        android:layout_width="200sp"        android:layout_height="200sp"        android:layout_centerInParent="true"        android:src="@drawable/splash"        android:id="@+id/imageView" />
    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/imageView"        android:layout_centerHorizontal="true"        android:layout_marginTop="24dp"        android:text="SG Developers"        android:textColor="@android:color/white"        android:textSize="30sp" />
</RelativeLayout>



Note : Here in This code splash is my Logo.


  • I have created my own gradient for background of my splash screen here is code for gradient background i.e. "bg.XML"


<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <gradient        android:startColor="#43c6d6"        android:centerColor="#39bece"        android:endColor="#2095a3"        android:angle="90"/>
</shape>

  • After This main thing is to write code this for Activity. Here is the code in 'Splash.java' file as below



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 Splash extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_splash);
        ImageView imageView = findViewById(R.id.imageView);        Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade);        imageView.startAnimation(animation);

        Thread timer = new Thread(){

            @Override            public void run() {

                try {
                    sleep(3000);                    Intent intent = new Intent(getApplicationContext(),MainActivity.class);                    startActivity(intent);                    finish();                    super.run();                } catch (InterruptedException e) {
                    e.printStackTrace();                }


            }
        };
        timer.start();    }
} 


  • After this the I have created animation file for this we need to create 'anim' folder in 'res' folder
  • after this inside new animation file we need to set the value of alpha for fade animation here is the code 


<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:fromAlpha="0.0"    android:toAlpha="1.0"    android:duration="4000">
</alpha>

  • After this only one thing is remaining that is we need to set theme for our activity by adding the following code inside the 'style.XML' we will get full screen Activity i.e. Action bar will be hidden

<resources>
    <!-- Base application theme. -->    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>    </style>

    <style name="splash" parent="Theme.AppCompat.DayNight.NoActionBar"/></resources>


  • Then only last step is remaining that is we need to this theme for our splash screen activity by writing the following code inside 'manifest.XML' file as shown below.

<activity    android:name=".Splash"    android:theme="@style/splash">    <intent-filter>        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />    </intent-filter></activity>

  • Now our Splash screen is ready.

I have published the code on my Github you will get it here.
Also for more updates you Can Subscribe to my Channel here.