I'm having an issue creating a ProgressDialog in my onCreateDialog() method.
The code is as follows:
Dialog dialog;
switch(id){
case CONNECTING:
dialog = new ProgressDialog(this);
dialog.setMessage("Connecting").setTitle("");
return dialog;
Eclipse throws me an error setMessage wouldn't be a valid Method of the type ProgressDialog, though I expect it to be there since the documentation for API8 (which I use) says so.
AFAIK the instantiation should be possible since ProgressDialog ihnerits from Dialog right?
Can someone help me at this? It's really weird.
You need to change your code to:
Dialog dialog;
switch(id){
case CONNECTING:
dialog = new ProgressDialog(this);
((ProgressDialog)dialog).setMessage("Connecting");
dialog.setTitle("");
return dialog;
Alliteratively, you can change dialog to type ProgresssDialog if you are always returning a ProgresssDialog, but I doubt it.
The issue is that Dialog doesn't have a setMessage method. Which is the type of the variable dialog.
Edit:
This line:
dialog.setMessage("Connecting").setTitle("");
Also looks wrong since setMessage() returns void.
Related
I would like to set theme of progressDialog. To create it, I use this code:
progressDialog = ProgressDialog.show(this, "Please Wait", "Loading dictionary file....", true, false);
I can't just write
progressDialog = new ProgressDialog(...);
progressDialog.(do_sth_with_dialog);
progressDialog.show(...)
because the show() method is static and I get compiler warning.
Is there any way to use available constants like
progressDialog.THEME_HOLO_DARK
to set the dialog theme?
I would also like to change the Dialog background and make the corners round (I don't want to change anything with the progressBar that is inside progressDialog. There is many tutorials here, but they usually describe how to create new class that extends progressDialog class.
Is there easier way to set THEME and BACKGROUND color of progressDialog?
Why I can access constants like progressDialog.THEME_HOLO_DARK if I cant use them?
ProgressDialog.show() are static methods, so you don't get a class instance of ProgressDialog that you can set properties on.
To get a ProgressDialog instance:
// create a ProgressDialog instance, with a specified theme:
ProgressDialog dialog = new ProgressDialog(mContext, ProgressDialog.THEME_HOLO_DARK);
// set indeterminate style
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// set title and message
dialog.setTitle("Please wait");
dialog.setMessage("Loading dictionary file...");
// and show it
dialog.show();
EDIT 8/2016:
Regarding the comments about deprecated themes, you may also use styles.xml and inherit from a base theme, e.g.:
<style name="MyProgressDialog" parent="Theme.AppCompat.Dialog">
</style>
the details on how to do this are already covered extensively elsewhere, start with https://developer.android.com/guide/topics/ui/themes.html.
Using themes and styles.xml is (in my opinion) a much cleaner and easier to maintain solution than hard-coding a theme when instantiating the ProgressDialog, i.e. set it once and forget it.
Then you can just do
new ProgressDialog(mContext);
and let your global theme/style provide the styling.
Sorry.. I'm working right now. Can't give full details. But here is the answer.
ProgressDialog progressDialog;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){
progressDialog = new ProgressDialog(new ContextThemeWrapper(context, android.R.style.Theme_Holo_Light_Dialog));
}else{
progressDialog = new ProgressDialog(context);
}
progressDialog.setMessage("Loading....");
progressDialog.show();
You cannot inflate ProgressDialog.
What you can do is while doing async task, you can show custom dialog which you can create by inheriting from Dialog class.
Also see how to set background image for progress dialog?
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.item_dialog);
Here is my method, it works fine and shows the Dialog.
public void showDialog(){
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.mylayout);
dialog.show();
}
I have a test project and I would like to test that the Dialog is showing up. I would like to apply the .isShowing() method. Something like this...
assertTrue(dialog.isShowing());
But I don't know how to get to the dialog variable within my test.
I am not using Robotium (this isn't an option for me).
I'm currently using the ActivityUnitTestCase to test with. If any more information is required please don't hesitate to ask.
EDIT
I have attempted to use the answer below by making the Dialog public
public Dialog getDiag(){
return dialog;
}
Using this answer: I have a new problem when I run showDialog() in the test, it breaks when it hits: dialog.show();
android.view.WindowManager$BadTokenException: * Unable to add window -- token null
Declare Dialog outside showDialog function and then implement a method which returns this Dialog instance.
public Dialog getDiag(){
return dialog;
}
and then do something like this
assertTrue(new YourClassName().getDialog().isShowing());
I'm begining with Android development. I have an asynchronous operation, but my intention is wait the operation is completed, and after that, continue the execution of my program.
I have used AsyncTask, and all its methods, but I got error, because on the onPreExecute method I want to show another Activity, so I guess I can't show another Activity. That's the reason I want to wait to complete the asynchronous operation.
Greetings
1st edit:
I've used AsyncTask(onPreExecute, doInBa..., onPost... ), but none method works. I understand how it works the AsyncTask class, but I want stop the execution when I invoke one asynchronous thirdparty method, because in the listener that needs, I change the value of a String variable X, and after invoke my method, that uses the thirdparty method, I use the variable X. I got an exeption because the variable hasn't updated.
Please, read and follow example of AsynTask. You need to override onPostExecute
Not entirley sure what the question is but AsynTask has a method that you can call after everything has completed. A good practice is to have a dialog or a spinner showing that work is happening then dismiss it.
class loadingTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
//Do work
}
protected void onPostExecute(String s) {
ShowProgress.dismiss();
Intent myIntent = new Intent(this, PostExAct.class);
startActivity(myIntent);
}
}
In your on create:
ShowProgress = ProgressDialog.show(MainActivity.this, "",
"Loading. Please wait...", true);
new loadingTask().execute("params here");
This will show a loading dialog and while the work is being done then will be dismissed when the work is finished.
I have an activity that queries a server database and returns a list of results...while querying the app displays a simple progressDialog on the onCreate method like so:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//display progress dialog while querying server for values
final ProgressDialog dialog = ProgressDialog.show(this,"","Retrieving listings please wait...");
dialog.setCancelable(true);
dialog.show();
If the user clicks on an item from the list then another activity placeDetails is opened. Once done a user can press the back button to go back to the previous activity which displays the listings.
When I tested it naturally it shows the above dialog and sends the query back to the server even though the listings can be seen in the background of the progressDialog.
What I want to know is how would I prevent the database being queried again and the above progressDialog from displaying when the user presses the back button.
Do I have to go down the caching route? or is there another way?
1.) To prevent the database from being queried again you can simply cache this data in a local SQLite database as Tom Dignan mentioned.
2.) To prevent the progressDialog from displaying when the user presses the back button, simply override the onBackPressed() method of the current activity (when back is pressed) and set an Intent to the activity that preceeds the progresDialog. I believe there's even a method to do this so that you won't be starting a new instance of that activity but simply accessing a cached version.
I managed to solve this issue the missing link was I did not know how to check for dialog windows the following code helped:
//first declare the dialog so its accessible globally through out the class
public class ListPlaces extends ListActivity {
ProgressDialog dialog;
then on the onCreate first check that a dialog exists or not
if(dialog == null){
dialog = ProgressDialog.show(this,"","Retrieving listings please wait...");
//display progress dialog while querying server for values
dialog.setCancelable(true);
dialog.show();
}
And the mistake was I was using
dialog.hide();
instead of
dialog.dismiss();
Thanks for the contributions
I have a really weird problem, which I am unable to debug so far...
Thing is...my app needs to download something to work. So in the beginning of the onCreate() method, I check if that something is already downloaded. If not, I pop a dialog up asking the user to download it.
if (!isInstalled) {
showDialog(DIALOG_INSTALL);
} else {
start();
}
Where start() method performs some other action. Now, that showDialog calls this:
builder = new AlertDialog.Builder(MyApp.this);
builder.setMessage("Would you like to install...")
.setCancelable(false)
.setPositiveButton("Install", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
aManager.install(MyApp.this);
}
});
dialog = builder.create();
return dialog;
My dialog is shown and I am clicking, so aManager.install() is called. I am passing the context because that aManager.install() pops up a ProgressDialog to show downloading progress and spawns a new thread in which everything is downloaded. So obviously before creating my dialog I make a Handler to receive the response from that aManager.install(). And the response MAY vary, because for example the internet connection isn't available (Exception raised and catched and listener called with different code).
Now, when that happens (Exception) I would like to call another dialog saying "something went wrong, would you like to retry?"...so another call to showDialog(DIALOG_REINSTALL) (this time with another code).
Thing is...the showDialog() gets called (I can verify this by logging) but the dialogs doesn't show up. Instead my application JUST HANGS!?!?!?
Does someone have a clue why it's doing this???? No exception raised, absolutely nothing from logcat, I can't tell WHERE it's hanging...just see that the method is called and the dialog should be displayed...
Thank you very much!!
Looks like you have a deadlock. I would put the download code on the separate thread e.g. use AsyncTask. In task.onPreExecute() you can dismiss 1st dialog and pop-up your progress dialog which you update by overwriting task.onProgressUpdate()
Use .show() instead of .create().