You have to do this with AsyncTask (Intelligent Back Thread) and ProgressDialog
AsyncTask allows the user interface thread to be used correctly and easily. This class allows you to perform background operations and publish results to a user interface thread without the need to manipulate threads and / or handlers.
An asynchronous task is determined by a calculation that runs on a background thread and the result of which is published to the user interface thread. An asynchronous task is defined by three generic types called Params, Progress and Result, and 4 steps called begin, doInBackground, processProgress and end.
4 steps
When an asynchronous task is running, the task takes 4 steps:
onPreExecute() , is called in the user interface thread immediately after the task is completed. This step is typically used to set up a task, for example by showing a progress bar in the user interface.
doInBackground(Params...) is called in the background thread immediately after onPreExecute () completes the execution. This step is used to perform background calculations, which can be time consuming. The parameters of the asynchronous task are passed to this step. The result of the calculation must be returned by this step and will be returned to the last step. This step can also use publishProgress (Progress ...) to publish one or more progress units. These values ββare published in the user interface stream at the onProgressUpdate (Progress ...) stage.
onProgressUpdate(Progress...) , is called in the user interface thread after calling publishProgress (Progress ...). The runtime is undefined. This method is used to display any form of progress in the user interface while background computing is still in progress. For example, you can use it to animate a progress bar or display logs in a text field.
onPostExecute(Result) , called in the user interface thread after the background calculation is complete. The result of calculating the background is passed to this step as a parameter. Threading Rules
There are several streaming rules that must be followed for this class to function properly:
A task instance must be created in the user interface thread. execute (Params ...) must be called in the user interface thread. Do not call onPreExecute (), onPostExecute (Result), doInBackground (Params ...), onProgressUpdate (Progress ...) manually. A task can be executed only once (an exception will be thrown when trying to perform a second execution).
Code example
What the adapter does in this example is not important, it is more important to understand that you need to use AsyncTask to display a dialog for progress.
private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > { ProgressDialog dialog; @Override protected void onPreExecute() { dialog = new ProgressDialog(viewContacts.this); dialog.setMessage(getString(R.string.please_wait_while_loading)); dialog.setIndeterminate(true); dialog.setCancelable(false); dialog.show(); } @Override protected ContactsListCursorAdapter doInBackground(Void... params) { cur1 = objItem.getContacts(); startManagingCursor(cur1); adapter1 = new ContactsListCursorAdapter (viewContacts.this, R.layout.contact_for_listitem, cur1, new String[] {}, new int[] {}); return adapter1; } protected void onPostExecute(ContactsListCursorAdapter result) { list.setAdapter(result); dialog.dismiss(); } }