Sunday, July 1, 2012

Create Loading Screen

LoadingScreen.java

package beanmatrix.loadingscreen;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;

public class LoadingScreen extends Activity {
    /** Called when the activity is first created. */
       private ProgressDialog pd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new LoadView().execute();   
      }

    private class LoadView extends AsyncTask<Void, Integer, Void>
    {
              @Override
              protected void onPreExecute()
              {
                     pd = new ProgressDialog(LoadingScreen.this);
                     pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                     pd.setTitle("Loading...");
                     pd.setMessage("please wait,application will be shortly loaded ...");
                     pd.setMax(500);
                     pd.setProgress(0);
                     pd.show();
              }
              @Override
              protected Void doInBackground(Void... params)
              {      try
                     {
                           synchronized (this)
                           {      int count = 0;
                                  while(count <= 5)
                                  {      this.wait(500);
                                         count++;
                                         publishProgress(count*50);
                                  }
                           }
                     }
                     catch (InterruptedException e)
                     {
                           e.printStackTrace();
                     }
                     return null;
              }
              @Override
              protected void onProgressUpdate(Integer... values)
              {
                     pd.setProgress(values[0]);
              }
              @Override
              protected void onPostExecute(Void result)
              {
                     pd.dismiss();
                     setContentView(R.layout.main);
              }
    }
}


main.xml

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
</LinearLayout>



No comments:

Post a Comment