Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Creating a Loading Screen That Will Appear Between Two Activities

In Chapter: Graphical User Interface
Author: Shraddha Shravagi ('sshraddha')
Published? true
FormatLanguage: WikiFormat


If you are getting an black screen before loading an activity here's how to avoid it.


Sometimes when your Activity is fetching some data from database or from internet then it takes time to load-up. To avoid this scenario we can have a simple Activity that shows a loading image instead of a black screen.

Your normal scenario may be like this: ProfileList(user selects one profile) -> Black Screen -> ProfileData Suppose you have an list activity (ProfileList) from which when user selects one then connection is established and later Profile(ProfileData) data is shown and if it takes time to load ProfileData then you introduce an LoadingScreen

ProfileList(user selects one profile) -> LoadingScreenActivity -> ProfileData


1. Create a LoadingScreen layout file

Here you create an screen which just shows loading text and an progress bar loading_screen.xml

  android:layout_width="fill_parent" 	android:gravity="center" android:orientation="vertical"
  android:layout_height="fill_parent" android:background="#E5E5E5">
   <TextView android:text="Please wait while your data gets loaded..."
		android:layout_width="wrap_content" android:layout_height="wrap_content"
  <ProgressBar android:id="@+id/mainSpinner1" android:layout_gravity="center" 
			android:layout_width="wrap_content" android:layout_height="wrap_content"

2. Create a LoadingScreen class file

public class LoadingScreenActivity extends Activity {

//Introduce an delay
    private final int WAIT_TIME = 2500;
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	System.out.println("LoadingScreenActivity  screen started");

	new Handler().postDelayed(new Runnable(){ 
	    public void run() { 
              //Simulating a long running task
	     System.out.println("Going to Profile Data");
	  /* Create an Intent that will start the ProfileData-Activity. */
              Intent mainIntent = new Intent(LoadingScreenActivity.this,ProfileData.class); 

     This will load the next activity once WAIT_TIME is elapsed

3. Open LoadingScreenActivity from say your List from onListItemClick event

Create an intent to launch loading screen activity

protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
Intent intent = new Intent(ProfileList.this, LoadingScreenActivity.class);

This is how you show an simple loading screen for 2500ms. After the completion of these many seconds automatically the next activity that you mentioned in the handler of LoadingScreenActivity will start. Thus the black screen which is displayed when you do some web activity can be avoided.


If you found this recipe useful, why not buy the book and have the whole collection always at hand?"