Splash Screen
Splash Screen is usually the first screen when App is opened,
In this tutorial we create a splash screen with animation.
Sample pictures
Java code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package com.example.SplashScreen; import android.content.Intent; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.TextView; public class SplashScreen extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_screen); StartAnim(); new Handler().postDelayed(new Runnable() { @Override public void run() { startActivity(new Intent(getApplicationContext(), MainActivity.class)); } }, 4000); } void StartAnim(){ TextView ta = findViewById(R.id.splash_text); Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate); ta.startAnimation(animation); } @Override protected void onPause() { super.onPause(); finish(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.example.SplashScreen; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } |
XML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" tools:context=".SplashScreen" android:background="@color/r1" android:gravity="center"> <TextView android:id="@+id/splash_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SPLASH SCREEN" android:textColor="@color/w" android:textSize="40sp" android:textStyle="bold" /> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Main activity" android:textAlignment="center" android:textSize="36sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </android.support.constraint.ConstraintLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" > <scale android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" android:pivotY="50%" android:pivotX="50%"/> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%"/> <alpha android:fromAlpha="0"/> </set> |