This example shows a simple but effective splash screen with animation that can be created by using Android Studio.
Create a new directory named anim in the res directory. Right-click it and create a new Animation Resource file named fade_in.xml:
Then, put the following code into the fade_in.xml file:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" >
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
Create an empty activity using Android Studio named Splash
. Then, put the following code into it:
public class Splash extends AppCompatActivity {
Animation anim;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
imageView=(ImageView)findViewById(R.id.imageView2); // Declare an imageView to show the animation.
anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in); // Create the animation.
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(this,HomeActivity.class));
// HomeActivity.class is the activity to go after showing the splash screen.
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
imageView.startAnimation(anim);
}
}
Next, put the following code into the layout file:
<?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:id="@+id/activity_splash"
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"
tools:context="your_packagename"
android:orientation="vertical"
android:background="@android:color/white">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:layout_weight="1"
android:src="@drawable/Your_logo_or_image" />
</LinearLayout>
Turn your Splash
activity into a launcher by adding the following code to the AndroidManifest file:
<activity
android:name=".Splash"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Then, remove the default launcher activity by removing the following code from the AndroidManifest file:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>