Splash Screen with Fade In Animation in Android using JAVA
Hello friends,we are now going to show how to make a fade-in animation on splash screen using JAVA. Make a new project and go to MainActivity.java and type the following code in it.
MainActivity.java :
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
Animation anim;
ImageView imageView;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayourParams.FLAG_FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.id.activity_main);
imageView = (ImageView) findViewById(R.id.imageView2); anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
anim.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation) {
startActivity(new Intent(MainActivity.this, NextActivity.class));
}
@Override
public void onAnimationRepeat(Animation animation) { }
});
imageView.startAnimation(anim);
}
}
Ater this, go to Projects tab and Right Click on res folder and make a new directory named anim.
Now, Right Click on anim and go to New -> Animation Resource File and make a new animation file name fade_in.xml
Type the following code in fade_in.xml
fade_in.xml :
<set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true">
<alpha
android:duration="3000"
android:fromAlpha="0.0"
andriod:interpolator="@android:anim/linear_interpolator"
android:toAlpha="1.0"/>
</set>
Now, go to Projects Tab, Right click on com.example.spalshscreen and go to NEW -> ACTIVITY -> EMPTY VIEWS ACTIVITY and create a new activity named as NextActivity with the following code.
NextActivity.java :
import android.os.Bundle;
public class NextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
}
}
Now, go to activity_main.xml and type the following code in it.
NextActivity.java :
<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/cardview_default_radius"
android:paddingLeft="@dimen/cardview_default_radius"
android:paddingRight="@dimen/cardview_default_radius"
android:paddingTop="@dimen/cardview_default_radius"
tools:context="com.example.splashscreen.NextActivity"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView2"
android:layout_weight="1"
android:src="@drawable/java_icon"/>
</LinearLayout>
Now, copy the Image you want to show on SPLASHSCREEN go to Projects Tab, paste the image in res -> drawable folder and name it as in activity_main.xml file(java_icon). The image will be shown on the splash screen.