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();
}
Related
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
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();
}
}
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();
}
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()
I need to show standard loading circle on half-opaque background above current layout (with background and buttons etc). I found ProgressBar and how it works, but I need to render it on existing layout, and that's the problem.
To be more clear - imagine login screen with some image and button aka "Register". On click I need to show partly visible black background and a loading circle.
Try and see if this works (use it as an inner class)
private class executeHospitalNameGet extends AsyncTask<String, Void, Void> {
private ProgressDialog Dialog = new ProgressDialog(SettingsActivity.this);
private Context mContext;
public executeHospitalNameGet(Context context) {
mContext = context;
}
protected void onPreExecute() {
Dialog.setMessage("Getting Hospitals...");
Dialog.setTitle("Requesting Hospital Name");
Dialog.show();
}
protected Void doInBackground(String... urls) {
//DO WORK HERE
return null;
}
protected void onPostExecute(Void unused) {
Dialog.dismiss();
}
}
You would call it with this
new executeHospitalNameGet(getApplicationContext()).execute();
Use a RelativeLayout, It will allow you to have overlapping Views, check out this link:
http://developer.android.com/reference/android/widget/RelativeLayout.html
I think you're looking for something like modal dialog with loading animation...
check it out this... http://developer.android.com/guide/topics/ui/dialogs.html#ProgressDialog