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

Author: Shraddha Shravagi
Published? true
FormatLanguage: WikiFormat

Problem:

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

Solution:

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

Discussion:

1. Create a LoadingScreen layout file

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

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  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"
		android:textColor="#000000">
   </TextView>
  <ProgressBar android:id="@+id/mainSpinner1" android:layout_gravity="center" 
			android:layout_width="wrap_content" android:layout_height="wrap_content"
			android:indeterminate="true" 
			style="?android:attr/progressBarStyleInverse">
   </ProgressBar>
	
</LinearLayout>

2. Create a LoadingScreen class file


public class LoadingScreenActivity extends Activity {

//Introduce an delay
    private final int WAIT_TIME = 2500;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	System.out.println("LoadingScreenActivity  screen started");
	setContentView(R.layout.loading_screen);
	findViewById(R.id.mainSpinner1).setVisibility(View.VISIBLE);

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

     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);
		startActivity(intent);
}

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.

idarwin 2012-03-25 11:04:18.379 The line this.Sleep(1000); shows that this code has never been compiled. "this." unambiguously refers to the anonymous inner Runnable, which is not a Thread and doesn't have the "sleep" (lower case) method. For now, in the printed book, I made it compilable by changing it to Thread.sleep() with a try/catch around it. Did you have something else in mind? Thanks. Ian