In this blog explain how to create Custom ProgressDialog in AsyncTask Application run Time.
frist ,you have to create AsyncTask class in MainActivity.java class. show below
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressdialog = new ProgressDialog(this);
progressdialog.setMessage("Please Wait...");
progressdialog.show();
progressdialog.setCancelable(false);
progressdialog.setCanceledOnTouchOutside(false);
}
@Override
protected String doInBackground(String... cname) {
try {
return null;
}
@Override
protected void onPostExecute(String result) {
if (progressdialog != null) {
progressdialog.dismiss();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Complete MainActivity.java
package com.Webnetware.tiffinindia;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;
import com.Webnetware.Sqlitedatabase.DatabaseAdapter;
import com.Webnetware.adapter.ConnectionDetector;
import com.Webnetware.resApi.RestAPI;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class My_Recipe extends Activity {
ProgressDialog progressdialog;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setTitle(" Recipe");
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
setContentView(R.layout.my_recipe);
new HttpAsyncTask().execute();
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressdialog = new ProgressDialog(My_Recipe.this);
progressdialog.setMessage("Please Wait...");
progressdialog.show();
progressdialog.setCancelable(false);
progressdialog.setCanceledOnTouchOutside(false);
}
@Override
protected String doInBackground(String... cname) {
try {
} catch (Exception e) {
Log.d("Exception", e.toString());
}
return obj_restapi.ResponseApi();
}
@Override
protected void onPostExecute(String result) {
try {
if (progressdialog != null) {
progressdialog.dismiss();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
0 comments