Skip to content Skip to sidebar Skip to footer

Progressdialog In Android

This is my activity code.. here I'm fetching data from the database through JSON and PHP... How can I display a progessDialog when the data is loading? Here is my activity code: pa

Solution 1:

you need to use asyncTask

AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called onPreExecute, doInBackground, onProgressUpdate and onPostExecute

privateclassxyzextendsAsyncTask<Void, Void, Void> {
    privatefinal ProgressDialog dialog = new ProgressDialog(main.this);

    protected void onPreExecute() {
        this.dialog.setMessage("Please Wait...");
        this.dialog.show();
        //code which load at prefix time

    }

    @OverrideprotectedVoid doInBackground(Void... arg0) {

            // make code which you want in backgroundreturnnull;
    }

    protected void onPostExecute(finalVoid unused) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();

        }

    }
}

and use this in your button click event or in main file ::

 new xyz().execute();

UPDATE:

package org.postandget;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.*;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

publicclassmainextendsActivity {
    TextView tv;
    String text;
  ProgressDialog  progressDialog;
    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);       
        tv  = (TextView)findViewById(R.id.textview);
        text    = "";
        tv.setText("hi parthi");                
       newmain.execute();
    }
    publicvoidpostData(Object JSONfunctions)throws JSONException{
        // Create a new HttpClient and Post HeaderHttpClienthttpclient=newDefaultHttpClient();
        HttpPosthttppost=newHttpPost("http://eeeee.com/ee/login.php");      
        JSONObjectjson=newJSONObject();
        try {
            // JSON data:           
            json.put("name", "Fahmi Rahman");
            json.put("position", "sysdev");         
            JSONArray postjson=newJSONArray();
            postjson.put(json);
            // Post the data:
            httppost.setHeader("json",json.toString());
            httppost.getParams().setParameter("jsonpost",postjson);

            // Execute HTTP Post Request
            System.out.print(json);
            HttpResponseresponse= httpclient.execute(httppost);
            // for JSON:if(response != null)
            {
                InputStreamis= response.getEntity().getContent();
                BufferedReaderreader=newBufferedReader(newInputStreamReader(is));
                StringBuildersb=newStringBuilder();
                Stringline=null;
                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                text = sb.toString();
            }

            tv.setText(text);

        }catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
    privateclassxyzextendsAsyncTask<Void, Void, Void> {
        privatefinalProgressDialogdialog=newProgressDialog(main.this);

        protectedvoidonPreExecute() {
            this.dialog.setMessage("Please Wait...");
            this.dialog.show();
            //code which load at prefix timetry {
                postData(savedInstanceState);          
            } catch (JSONException e) {
                e.printStackTrace();        }
        }

        @Overrideprotected Void doInBackground(Void... arg0) {

                // make code which you want in backgroundreturnnull;
        }

        protectedvoidonPostExecute(final Void unused) {
            if (this.dialog.isShowing()) {
                this.dialog.dismiss();

            }

        }
    }
}

Solution 2:

First create an object of Progress Dialog in your activity like this ---->

  1. ProgressDialog pd;
  2. static final int Dialog_id = 1;

then within your Try block where you posting the data show the progress dialog like this ---->

  1. showDialog(Dialog_id);

and after your onCreate() method create an onCreateDialog() method like this ----->

4.

protected Dialog onCreateDialog(int id){
       switch(id){
          case Dialog_id :
            ProgressDialogpd=newProgressDialog(this);
            pd.setTitle("Loading Data");
            pd.setCancelable(true);
            return pd;
            break;
         returnnull;
}

Post a Comment for "Progressdialog In Android"