Animated Splash Screen to you Android App using Android Studio
Today I am switching you to one finest technology of this world and that is Android. In this tutorial I am showing you how you can create a animated Splash screen which is the first part of any android application creation.
When we are use Web Services to handle the web data and all that some time possible that data will come slow and it will take some time to load data that time viewers will not happy to see blank screen so that time splash screen will help us. So now start the creation of splash screen.
First Create Splashscreen.java class in your demo project of IDE or Android Studio and copy the whole code except package name form their.
import android.app.Activity; import android.content.Intent; import android.graphics.PixelFormat; import android.os.Bundle; import android.view.Window; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.LinearLayout; public class Splashscreen extends Activity { public void onAttachedToWindow() { super.onAttachedToWindow(); Window window = getWindow(); window.setFormat(PixelFormat.RGBA_8888); } /** Called when the activity is first created. */ Thread splashTread; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splashscreen); StartAnimations(); } private void StartAnimations() { Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha); anim.reset(); LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay); l.clearAnimation(); l.startAnimation(anim); anim = AnimationUtils.loadAnimation(this, R.anim.translate); anim.reset(); ImageView iv = (ImageView) findViewById(R.id.splash); iv.clearAnimation(); iv.startAnimation(anim); splashTread = new Thread() { @Override public void run() { try { int waited = 0; // Splash screen pause time while (waited < 3500) { sleep(100); waited += 100; } Intent intent = new Intent(Splashscreen.this, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); Splashscreen.this.finish(); } catch (InterruptedException e) { // do nothing } finally { Splashscreen.this.finish(); } } }; splashTread.start(); } }
The activity’s onCreate() method calls setContentView() passing the layout that includes the fragment definition. Before that method returns: The hosted fragment’s constructor is called. The fragment’s onInflate method is called. The fragment’s onAttach method is called.
if this not cleared your doubt please check this link http://www.silverbaytech.com/2014/03/24/android-fragment-lifecycle-in-detail/
After create Java class of splash screen then create activity layout file in your layout folder of project. and the name of this layout file is activity_splashscreen.xml then copy the code .
layout/activity_splashscreen.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:layout_gravity="center" android:id="@+id/lin_lay" android:gravity="center" android:orientation="vertical" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/splash" android:background="@drawable/splash_img" /> </LinearLayout>
Make sure your id same as your mention in your Java class otherwise it will create error in App
Now start Animation part in your splash screen in this splash screen I am using down to up image with some delay. For that create anim(resource directory) in your project and inside of this create
alpha.xml and translate.xml file.
anim/alpha.xml
<?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="3000" />
anim/translate.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0%" android:toXDelta="0%" android:fromYDelta="200%" android:toYDelta="0%" android:duration="2000" android:zAdjustment="top" /> </set>
Now your code part is completed. Just add your Logo or what every image your want to add on your splash screen add it in drawable folder of project with splash_img.png name because in our layout file we mention this name inside of imageview tag.
After that your splash screen is ready. Enjoy your work Happy coding 🙂
It’s nice and easy understandable….
Thanks
this code is not work on my project to go second acivity
Share android studio error msg.
Thanks for this code. love from brazil