Android: Progress Dialog in AsyncTask not showing up - android

I am starting the asynctask inside a SherlockListFragment which was created inside a SherlockFragmentActivity as a tab.
I pass the asynctask constructor my activity context and initialize the asynctask like this inside onCreate():
AsyncTask<String, Integer, String[]> asynctask = new DownloadFilesTask(getSherlockActivity()).execute(url);
The constructor inside the AsyncTask class DownloadFilesTask looks like this:
private ProgressDialog dialog;
private SherlockFragmentActivity activity;
public DownloadFilesTask(SherlockFragmentActivity activity) {
this.activity = activity;
this.dialog = new ProgressDialog(activity);
}
Pre-execute and post execute look like this:
protected void onPreExecute(){
Log.d("AsyncTask!", "Showing dialog now!"); //shown in logcat
dialog.setMessage("Retrieving all currently airing anime. Please wait.");
dialog.setCancelable(false);
dialog.show();
}
.
protected void onPostExecute(String[] result) {
Log.d("AsyncTask!", "Dismissing dialog now!"); //shown in logcat
dialog.dismiss();
}
But the progress dialog doesn't show up while all the background work is being done!
What am I doing wrong here? I think it might be a context problem.

Part of the problem was fixed thanks to the comment from Mike Repass about passing a plain old context.
As for the dialog not showing up...I was just being stupid because I called a .get() after the execute OUTSIDE the AsyncTask which blocks the UI thread. Obviously the dialog is not going to show up that way.

In Java "If your method overrides one of its superclass's methods, you can invoke the overridden method through the use of the keyword super." Therefore change your onPreExecute() method when you start progress dialog to:
#Override
protected void onPreExecute(){
super.onPreExecute();
dialog = new ProgressDialog(activity);
Log.d("AsyncTask!", "Showing dialog now!"); //shown in logcat
dialog.setMessage("Retreiving all currently airing anime. Please wait.");
dialog.setCancelable(false);
dialog.show();
}

Related

How to put Progress dialog in seperate class and call in every activity in android?

I have a progress dialog in my every activity and in every activity I write code for progress dialog with different message where I want.Is there any way to put progress dialog code in seperate class and call that class in activity where I want to show that progress dialog.
here is my code for progress dialog:-
ProgressDialog m_Dialog = new ProgressDialog(CLoginScreen.this);
m_Dialog.setMessage("Please wait while logging...");
m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
m_Dialog.setCancelable(false);
m_Dialog.show();
You can define a class to encapsulate this operation and maybe some other involving dialogs. I use a class with static methods, something like this:
public class DialogsUtils {
public static ProgressDialog showProgressDialog(Context context, String message){
ProgressDialog m_Dialog = new ProgressDialog(context);
m_Dialog.setMessage(message);
m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
m_Dialog.setCancelable(false);
m_Dialog.show();
return m_Dialog;
}
}
In the Activity class:
ProgressDialog myDialog= DialogUtils.showProgressDialog(this,"some message");
...
myDialog.dismiss();
Of course you can add others parameters to the operation so it can be more flexible.
Hope it helps.
Create a method in global class and pass activity instance like this
public ProgressDialog showDailog(Context con)
{
ProgressDialog m_Dialog = new ProgressDialog(con);
m_Dialog.setMessage("Please wait while logging...");
m_Dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
m_Dialog.setCancelable(false);
return m_Dailog;
}
in activity class
ProgressDialog mDailog;
ABCClass obj = New ABCClass(CLoginScreen.this);
mDailog = obj.showDailog();
mDailog.show(); //you can use this line where you want to show dailog in Activity class

Android - Showing progress dialog in a separate class which extends AsyncTask

I have a class which is called myAPI, used for getting information from server db. This class has several classes used to get individual information, for example getCourseInformation, login, register, and many more.
It worked well until a point which I realized that I really need a ProgressDialog. An Indeterminate ProgressDialog to be exact.
Tried doing this :
ProgressDialog progressDialog;
Context context;
public myAPI(Context context)
{
this.context= context;
}
public myAPI()
{
}
To get the Context of the class from which the myAPI is called.
And below is how I use it in one of my class.
public class login extends AsyncTask<MyLogin, String, String>{
#Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(true);
progressDialog.show();
}
...
}
But it doesn't work. It says Unable to add window -- token null is not for an application
Debugged it, and I could see that Context is there.. So it shouldn't be about that.. Any guide would be much MUCH appreciated!
Instead of use context, try to use activity
#Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(true);
progressDialog.show();
}

Android, Change progress dialog background color?

I have a progress dialog fully defined by java code. I just want to change the background color and probably text color. I've seen other posts in stackoverflow like adding style.xml and so on, but none worked. Please don't refer me to other posts. What to do?
My code sounds like this:
Class A extends Activity {
ProgressDialog pd;
Context context;
public void onCreate(){
context = A.this;
pd = new ProgressDialog.show(context, "" , "Loading");
}
}
![A sample of progress dialog which i want. Actually i have it
, but the problem is the background color which i want to be white.]1
pd = new ProgressDialog.show(context, "" , "Loading");
pd.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.RED));
Do this in your code:
Class A extends Activity {
ProgressDialog pd;
Context context;
public void onCreate(){
context = A.this;
pd = new ProgressDialog(context, android.R.style.Theme_DeviceDefault_Light_Dialog_Alert);
pd.setTitle("");
pd.setMessage("Loading");
pd.show();
}
}

ProgressDialog do not dissmiss() in AsyncTask

I want to show a ProgressDialog in AsyncTask.
This run fantastic. But if i call mLoginPD.dissmiss() in onPostExecute() do not run.
The ProgressDialog is always on the screen.
Here is my code:
SherlockActivity mActivity;
ProgressDialog mLoginPD;
public Task_Login(String name, String pass, SherlockActivity activity) {
this.passwort = pass;
this.benutzername = name;
this.mActivity = activity;
}
protected void onPreExecute() {
super.onPreExecute();
mLoginPD = new ProgressDialog(mActivity);
mLoginPD.show(mActivity, "Login", "Logge Spieler ein...");
}
protected void onPostExecute(Void result) {
Log.e("hello", "hello");
mLoginPD.dismiss();
mLoginPD.cancel();
if(mLoginPD.isShowing()) {
mLoginPD.dismiss();
}
}
onPostExecute() calls. I can see "hello" in LogCat.
(I have doInBackground() but i is irrelevant)
The problem is that you're creating two ProgressDialog objects.
This line:
mLoginPD = new ProgressDialog(mActivity);
creates a dialog and assigns it to mLoginPD, but does not show it.
This line:
mLoginPD.show(mActivity, "Login", "Logge Spieler ein...");
creates another dialog and shows that one. The problem is that show() is a static method that creates and shows a dialog all in one. So it's creating a second one separate from mLoginPD which is shown. mLoginPD is never shown, so calling dismiss() or cancel() doesn't do anything.
What you need to do is this:
mLoginPD = ProgressDialog.show(mActivity, "Login", "Logge Spieler ein...");
in place of both those lines. This uses show() to create and show the dialog and assign it to mLoginPD so you can dismiss it later.
If you're overriding onPreExecute, i dont think you're supposed to call super.onPreExecute()?
The answer from Geobits istn running too. Always show a NullPointerException.
Here is the code to solve my problem:
mLoginPD = new ProgressDialog(mActivity);
mLoginPD.setTitle("Login");
mLoginPD.setMessage("Logge Spieler ein...");
mLoginPD.show();
than i can call mLoginDP.dismiss() or cancel() in onPostExecute()

How to change Show/Remove Dialog and onPrepareDialog for DialogFragments

I'm working on an Android project. I need to use Android 1.6 or above.
My project was working, but now it is showing me some warnings about Dialogs like
"The method dismissDialog(int) from the type Activity is deprecated"
"The method showDialog(int) from the type Activity is deprecated", etc.
So I want to "update" my project to solve these warnings.
I have read and made some test projects to learn about Fragments and DialogFragment.
I have created my own ProgressDialog and I want to use it on my real project, but I have some problems.
public class MyProgressDialog extends DialogFragment {
public MyProgressDialog(){
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Context context = getActivity();
ProgressDialog dialog = new ProgressDialog(context);
Resources resources = context.getResources();
String message = resources.getText(R.string.wait).toString();
dialog.setMessage(message);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
return dialog;
}
}
Earlier in my project, I created the ProgressDialog and then, in onPrepareDialog() method, I called an AsyncTask to connect the server, downloaded the data, etc. Then in onPostExecute of the AsyncTask, I dismissed the ProgressDialog and started the new Activity. But now I can't do that because onPrepareDialog is deprecated.
Calling ActionAsyncTask on onPrepareDialog of Activy
#Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch(id){
case Constants.PROGRESS_DIALOG:
new ActionAsyncTask().execute();
break;
}
}
onPostExecute of ActionAsyncTask
#Override
protected void onPostExecute(Integer result) {
dismissDialog(Constants.PROGRESS_DIALOG);
}
How can solve this? What is the right way to do this? I want to write the best code for this, the most efficient code.
Thanks.

Categories

Resources