Android: Progress Dialog shows a black screen - java

This is my code that am I using to implement a ProgressDialog when a camera fired and photo taken.
public void onPhotoTaken() {
_taken = true;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(_path, options);
try {
ProgressDialog dialog = ProgressDialog.show(this, "Loading", "Please wait...", true);
ExifInterface exif = new ExifInterface(_path);
int exifOrientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
On clicking on the save button of my android device, instead of displaying the Progress Dialog, it displays a black screen. Please what could be wrong.

You should remember when it comes to Progress dialog is that you should run it in a separate thread. If you run it in your UI thread you'll see no dialog.
If you are new to Android Threading then you should learn about AsyncTask. Which helps you to implement painless Threads.
Sample code:
private class CheckTypesTask extends AsyncTask<Void, Void, Void>{
ProgressDialog asyncDialog = new ProgressDialog(IncidentFormActivity.this);
String typeStatus;
#Override
protected void onPreExecute() {
//set message of the dialog
asyncDialog.setMessage(getString(R.string.loadingtype));
//show dialog
asyncDialog.show();
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... arg0) {
//don't touch dialog here it'll break the application
//do some lengthy stuff like calling login webservice
return null;
}
#Override
protected void onPostExecute(Void result) {
//hide the dialog
asyncDialog.dismiss();
super.onPostExecute(result);
}
}
Good luck.
EDIT
The method show() must be called from the User-Interface (UI) thread, while doInBackground() runs on different thread which is the main reason why AsyncTask was designed.
You have to call show() either in onProgressUpdate() or in onPostExecute

It is working for me just using your ProgressDialog line of code so the problem must be elsewhere but you can try:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Please wait...");
dialog.setTitle("Loading");
dialog.show();

Related

ProgressDialogue is not dismissing or cancelling at all

I have shown one progress bar on API call like below :
// prepare for a progress bar dialog
progressBar = new ProgressDialog(context);
progressBar.setCancelable(false);
progressBar.setMessage(context.getResources().getString(R.string.please_wait));
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
But when I cancel or dismiss the progress dialogue created above it has no effect.
progressBar.cancel();
progressBar.dismiss();
Above two calls are in success & failure callback methods :
#Override
public void success(RockAPI.CallResult result) {
progressBar.cancel();
progressBar.dismiss();
....
}
#Override
public void failure(RockAPI.CallResult result) {
progressBar.cancel();
progressBar.dismiss();
......
}
I debugged the application at both success & failure points these lines of code getting executed but still progress dialogue persists. I have checked all the code there is no other place from which this progress bar show() is getting called. Its the same progress dialogue but just not getting canceled.
create such alike functions and call whenever needed..
ProgressDialog progressDialog;
public void showPD(String message) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(getContext());
//progressDialog.setProgressNumberFormat(null);
//progressDialog.setProgressPercentFormat(null);
//progressDialog.setIndeterminate(true);
//progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(message);
progressDialog.setCancelable(false);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
}
public void hidePD() {
if (progressDialog != null) {
progressDialog.dismiss();
progressDialog = null;
}
}
You are setting cancelable as false so dont call progressBar.cancel(). Skip that and directly call progressBar.dismiss(). It should work. If it doesnt we will dig deeper.
Use true instead of false in your setCancelable and it will work .
progressBar.setCancelable(true);

Show DialogFragment on AsyncTask.onPostExecute when activity is in background;

I got a java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState when trying to show a dialog after the Activity goes to background, I need to show this message to the user, is there any methods to show the dialog when activity is resumed. My code is something like this:
public class MyTask extends AsyncTask<String,String,String>{
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
#Override
protected void onPreExecute() {
progressDialog.setMessage(getString(R.string.dialog_progress_message));
progressDialog.setCancelable(false);
progressDialog.show();
}
#Override
protected String doInBackground(String... params) {
// HTTP POST user action
// App goes to background (user press home button i.e.)
// Getting message from server
return message;
}
#Override
protected void onPostExecute(String message) {
progressDialog.dismiss();
DialogAlert dialogAlert = DialogAlert.newInstance(message);
dialogAlert.setCancelable(false);
//App crash here
dialogAlert.show(getSupportFragmentManager(), TAG);
}
}
I have tried with AsyncTaskLoader but the dialog doesn't show up when activity resumes. Any ideas.
Try with below code snippets,
Replace
dialogAlert.show(getSupportFragmentManager(), TAG);
with
getSupportFragmentManager().beginTransaction().add(dialogAlert, "tag")
.commitAllowingStateLoss();

Android ProgressDialog not showing (blocked by code in other Thread)

Looking to similar questions everybody solve the problem of not appearing their Progress Dialog putting the intermediate code in a separate Thread.
My problem is that the mentioned solution is not working for me.
In my activity:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.dialog_ddbb_download_text)
.setPositiveButton(R.string.Accept, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// In this method I show the progress dialog
showProgressAndDownloadDDBB();
}
})
.setNegativeButton(R.string.Cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
builder.create().show();
Method in the activity:
private void showProgressAndDownloadDDBB() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
// Here I call the Runnable to execute the code in other Thread and let the UI draw the Progress Dialog. If it wasn't called, the progress dialog does appear.
DDBB_Download_Manager ddbb_download_manager = new DDBB_Download_Manager(mContext, progressDialog);
ddbb_download_manager.run();
}
My runnable class, expected to run the intermediate code in a separate Thread:
public class DDBB_Download_Manager implements Runnable {
public DDBB_Download_Manager(Context context, ProgressDialog progressDialog) {
this.mContext = context;
this.mProgresDialog = progressDialog;
}
#Override
public void run() {
someCode()
Thread.sleep(3000);
// The GUI shows the accept Button clicked for 3 seconds (like it was freezed)
// Here I try to hide the Progress dialog after finishing the job, but it doesn't matter becasuse the progress dialog didn't even show up.
View rootView = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content);
rootView.post(new Runnable() {
#Override
public void run() {
mProgresDialog.dismiss();
}
});
}
So the question is:
if I am executing the code between the Show and Dismiss methods of the Progress Dialog in a different Thread than the UI Thread, why is not the dialog showing up?
In fact it appears If I don't call the Runnable.
That is because you are running directly the dismiss() method from the runnable when you call ddbb_download_manager.run() where the progress dialog is cleared/done and if you are not calling it then the progress dialog will show due to that dismiss is on yet been called.
Make sure that you call ddbb_download_manager.run() when you want your progress dialog to be dismissed. don't call it directly after you show your progress dialog.
private void showProgressAndDownloadDDBB() {
progressDialog = new ProgressDialog(mContext);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
// Here I call the Runnable to execute the code in other Thread and let the UI draw the Progress Dialog. If it wasn't called, the progress dialog does appear.
DDBB_Download_Manager ddbb_download_manager = new DDBB_Download_Manager(mContext, progressDialog);
Handler handler = new Handler();
handler.postDelayed(ddbb_download_manager ,3*1000);
}

ProgressDialog not being displayed in Activity with custom View

I am developing an Android app which has 2 classes. Game, which extends Activity, and GameView, which extends View.
When the game is loaded, it sets the content view to GameView, which is just a drawing class that uses a canvas to display the game.
I am trying to create a ProgressDialog in the Game class which will show a spinner after a certain action has been done, as this takes a long time to complete. So far I have the following:
setContentView(R.layout.activity_game);
ProgressDialog pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Calculating hint");
pd.show();
AsyncTask<String[][], Void, SudokuSquare> nextSquareThread = new GetNextSquare().execute(puzzleNumbers);
next = nextSquareThread.get();
pd.dismiss();
setContentView(gameView);
And my AsyncTask class looks like this:
private class GetNextSquare extends AsyncTask<String[][], Void, SudokuSquare> {
private ProgressDialog dialog = new ProgressDialog(Game.this);
#Override
protected void onPreExecute() {
this.dialog.setMessage("Finding next number");
this.dialog.show();
}
#Override
protected SudokuSquare doInBackground(final String[][]... args) {
try {
SudokuAdvancedSolver solver = new SudokuSolver(args[0]);
return solver.getOneValue();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
#Override
protected void onPostExecute(final SudokuSquare result) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
At the moment I have two ProgressDialogs, one inside the AsyncTask and one outside. Which one is correct? Also, the spinner is not being displayed at all. What am I overlooking which is causing this to be the case?
only the one outside is correct. because you are trying the main thread (the UI thread of your activity) by another thread (your asychronic task). you should use a handler in place of this :
1/ you show the progress bar
2/ you load the game in a thread
3/ when the game is loaded you send a message to the handler which will stop the progress bar.
See this exemple . you should dismiss your dialog in the handler (when the handler receives the message from the thread) .
If you don't implement a listener on Asynctask, i could suggest you to dismiss your progress dialog onPostExecute
private ProgressDialog dialog;
public void setProgressDialog(ProgressDialog dialog){
this.dialog = dialog;
}
#Override
protected void onPostExecute(final SudokuSquare result) {
dialog.dismiss();
}
and before you executing Asynctask add this code
nextSquareThread.setProgressDialog(pd);

Android display progressdialog while loading URLs

My problem is really weird. The scenario is pretty simple: I wan't to show a progress Dialog as long as my activity is loading urls contents.
First I've tried to display a ProgressDialog just on its own, without any special functions inside. Worked well. But as soon as I added a function which loads urls, the program first loaded those urls and then displayed the progress bar. Here's my code:
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setMessage("Logging in ...");
progDialog.show();
client = new GameClient(context, universe, username, password);
client.login();
progDialog.dismiss();
Nothing special. But for some reason the activity first make the "login-part" and then tries to show the dialog, but it doesn't show up anyhow ...
Could you give me hints to solve the problem?
You need to run the login in a separate Thread so it doesn't block the UI thread, like this
...
progDialog.show();
new Thread(new Runnable() {
#Override
public void run() {
client = new GameClient(context, universe, username, password);
client.login();
progDialog.dismiss();
}
}).start();
or use an AsyncTask. See http://developer.android.com/resources/articles/painless-threading.html
Use ProgressDialog Class to show it.
/*****************************************
* AsyncTask Class to Parse and Display
******************************************/
class AsyncTaskClassName extends AsyncTask<Void,Void, Void>{
ProgressDialog progressDialog = null;
/* ***********************************
* Pre-Execute Method
* ********************************** */
#Override
protected void onPreExecute() {
progressDialog = util.getProgressDialog(ActivityClassName.this, "Please wait...", "Parsing List... ");
//ActivityClassName -> The Name of the Activity Class you want to show ProgressDialog
// progressDialog.hide();
progressDialog.show();
/* Do your Pre-Execute Configuration */
}
/* ***********************************
* Execute Method
* ********************************** */
#Override
protected Void doInBackground(Void... arg0) {
/* Do yourxec Task ( Load from URL) and return value */
return null;
}
/* ***********************************
* Post-Execute Method
* ********************************** */
#Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
/* Do your Post -Execute Tasks */
}

Categories

Resources