Android Splash screen is an Activity screen which loads first at the time of starting application. Some Application may have a splash screen and some may not. The reason behind showing a splash screen is to show some information before the app is starting or if we want to sync some data from server before starting application ,for managing that time we can make use of a splash screen.
In this tutorial we will learn how to implement a splash screen with a timer, for starting we need to have an ImageView in your splash screen layout file.Add the following ImageView to your splash screen
<ImageView android:id="@+id/im_background" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/your image name"/>
Next step is to create an “anim” folder under res, and create a file zoom_in.xml
zoom_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="3000" android:fromXScale="1" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.5" android:toYScale="1.5" > </scale> </set>
Now simply add the following code to your SplashScreen Activity file
public class Splash extends AppCompatActivity { Timer myTimer; int count=0; ImageView im_background; private Animation animZoomIn; boolean flag = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_splash); animZoomIn = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom_in); getSupportActionBar().hide(); im_background = (ImageView) findViewById(R.id.im_background); im_background.startAnimation(animZoomIn); myTimer = new Timer(); myTimer.schedule(new TimerTask() { @Override public void run() { count = count + 1; if(count> 5) { if(!flag){ flag = true; Intent intent = new Intent(Splash.this, MainActivity.class); startActivity(intent); finish(); } } } }, 0, 1000); } @Override protected void onDestroy() { myTimer.cancel(); super.onDestroy(); } @Override protected void onPause() { myTimer.cancel(); super.onPause(); } }
In the above code i am giving some animation as well to background image until it is taken to
next screen