Showing a progress dialog in Android app - android

I want to use a progress dialog in an activity named myActivity.
I launch it from a method in the activity:
progressDialog = ProgressDialog.show(myActivity.this, "", "loading ...");
but nothing appears. Why?
I've also tried this line:
progressDialog = ProgressDialog.show(myActivity.this, "", "loading ...",true);
with the same result.

Just this
//Declare progressDialog before so you can use .hide() later!
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();

Please add runnable thread into your code
Example: https://abhiandroid.com/ui/progressdialog
or see this example code
progressDialog.show(); // Display Progress Dialog
progressDialog.setCancelable(false);
new Thread(new Runnable() {
#Override
public void run() {
try {
while (progressDialog.getProgress() <= progressDialog.getMax()) {
Thread.sleep(200);
handle.sendMessage(handle.obtainMessage());
if (progressDialog.getProgress() == progressDialog.getMax()) {
progressDialog.dismiss();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}}).start();

Related

How to show Progress dialog on custom Dialog?

I am getting image from server and showing it on custom dialog and I want to show progress dialog while image is fetching.
I am using this
loadingDialog = ProgressDialog.show(getActivity(), "", "Loading. Please wait...", true);
but it Hides under the custom dialog.
Put this code in call of fetching an image from server
try {
progDialog = new ProgressDialog(Activity_FindInfo.this);
progDialog.setIndeterminate(true);
progDialog.setMessage("loading");
progDialog.setCancelable(false);
progDialog.show();
} catch (Exception ex) {
}
declare this in your class
private ProgressDialog progDialog;
after that add following code just before call to your fetching image..
try {
progDialog = new ProgressDialog(yourCustomDialog.getContext());
progDialog.setIndeterminate(true);
progDialog.setMessage("fetching image...");
progDialog.setCancelable(false);
progDialog.show();
} catch (Exception ex) {
}
then when image is fetched before that add this to dismiss dialog..
try {
if (progDialog != null && progDialog.isShowing()) {
progDialog.dismiss();
}
} catch (Exception ex) {
}

Setting text on ProgressDialog crashes app

I'm learning android and I don't know why this code doesn't work. Can you tell me why it doesn't work and take me correct code?
final ProgressDialog dialog = ProgressDialog.show(LoginScreen.this, "", "Loading. Please wait...", true);
Thread loggingStatus = new Thread() {
public void run() {
try
{
sleep(2000);
dialog.setMessage("Logging in. Please wait.");
sleep(2000);
dialog.dismiss();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
};
loggingStatus.start();
You have to move the portion of the background task that updates the ui onto the main thread. There is a simple piece of code for this:
putting runOnUiThread( new Runnable(){ .. inside run():
final ProgressDialog dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
Thread loggingStatus = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
sleep(2000);
dialog.setMessage("Logging in. Please wait.");
sleep(2000);
dialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
};
loggingStatus.start();
You should not set the Text of ProgressDialog in background thread . All UI updation should be done only on UI thread(Main thread)
Either use AsyncTask or Handler for this logic or functionality

Why Progress Dialog is not open while clicking on Submit Button

I have set the IP of Ethernet. here i am creating the file on a specific path and run the code of IP set i.e.,sudo.
Everything works well but It is not showing the progress dialog box on the click of the submit button but all other functions mentioned in the setOnClickListener are working properly.
Can anybody help me.
submt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validationIP();
if (var == true) {
ProgressDialog progressdialog = new ProgressDialog(Third_Ethernet_Layout.this);
progressdialog.setMessage("Please Wait....");
progressdialog.show();
progressdialog.setCancelable(false);
progressdialog.setCanceledOnTouchOutside(false);
try {
File file = new File(filepath);
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
write();
sudo(ipfetch, netmaskfetch, gatewayfetch, dns1fetch, dns2fetch);
progressdialog.dismiss();
finish();
Toast.makeText(Third_Ethernet_Layout.this, "Ethernet IP Change Successfully", Toast.LENGTH_SHORT).show();
}
}
});
You are using progressdialog.dismiss(); so that progressdialog is dismissed.
You should user asunc Task for it
private class AsyncAboutUs extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressdialog = new ProgressDialog(Third_Ethernet_Layout.this);
progressdialog.setMessage("Please Wait....");
progressdialog.show();
progressdialog.setCancelable(false);
progressdialog.setCanceledOnTouchOutside(false);
}
#Override
protected Void doInBackground(Void... strings) {
try {
File file = new File(filepath);
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
write();
sudo(ipfetch, netmaskfetch, gatewayfetch, dns1fetch, dns2fetch);
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (!isCancelled()) {
finish();
}
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
ON Button Click :
submt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
validationIP();
if (var == true) {
new AsyncAboutUs().execute();
}
}
});
The code for showing progress dialog is working fine, may be the process is fast and thats why the progress dialog is not visible.
can try using a thread sleep to actually see if there is an issue with it.
It does show the progress but you hide it immediately by calling dismiss() and further by finish().
However, doing your heavy task inside the handler is an incorrect way to achieve what you want. It would not work as you think anyway. What will happen is that your code will block UI thread inside handler and no progress will be shown (and application will potentially be killed if you hold long enough).
The correct way to do this is to implement an AsyncTask, there is a straightforward code example in this documentation link. You need to show() progress dialog, execute the async task, perform your file etc. code in doInBackground() and update progress values by publishProgress on the way.
In the onProgressUpdate(), update the dialog or required fields and, finally, in onPostExecute do the finish() or other actions you wish on completion.

Progress Dialog Message Text is not visible in Android-M

Message text of progress dialog is not visible. Please see the attached screenshot.
Code:
dialog = ProgressDialog.show(this, "", getResources().getString(R.string.scan_devices), true);
Please help me to resolve this issue.
Thanks.
ProgressDialog progressDialog = ProgressDialog.show(ExtractCallData.this, "title", "Message",true);
Try this :
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();
// To dismiss the dialog
progress.dismiss();
OR
ProgressDialog.show(this, "Loading", "Wait while loading...");
progressDialog = new ProgressDialog(mContext);
progressDialog.setIndeterminate(true);
progressDialog.setMessage(getResources().getString(R.string.scan_devices));
progressDialog.show();
Above what ever you are used was work properly in even Android-M also, I think problem with Context you are passed use proper context. Once try bellow one also
public static ProgressDialog progress(Context context, String msg, boolean isCancelable) throws Exception {
try {
final ProgressDialog mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage(msg);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setCancelable(isCancelable);
mProgressDialog.setCanceledOnTouchOutside(isCancelable);
mProgressDialog.show();
mProgressDialog.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mProgressDialog.dismiss();
}
return false;
}
});
return mProgressDialog;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}

ProgressDialog dismissal in android

I want to open a ProgressDialog when I click on the List Item that opens the data of the clicked Item form the Web Service.
The ProgressDialog needs to be appeared till the WebContent of the clicked Item gets opened.
I know the code of using the Progress Dialog but I don't know how to dismiss it particularly.
I have heard that Handler is to be used for dismissing the Progress Dialog but I didn't found any worth example for using the Handler ultimately.
Can anybody please tell me how can I use the Handler to dismiss the Progress Dialog?
Thanks,
david
Hi this is what you want
public void onClick(View v)
{
mDialog = new ProgressDialog(Home.this);
mDialog.setMessage("Please wait...");
mDialog.setCancelable(false);
mDialog.show();
new Thread(new Runnable()
{
#Override
public void run()
{
statusInquiry();
}
}).start();
}
here is the web webservice that is called
void statusInquiry()
{
try
{
//calling webservice
// after then of whole web part you will send handler a msg
mHandler.sendEmptyMessage(10);
}
catch (Exception e)
{
mHandler.sendEmptyMessage(1);
}
}
and here goes handler code
Handler mHandler = new Handler()
{
public void handleMessage(android.os.Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case 10:
mDialog.dismiss();
break;
}
}
}
};
A solutiion could be this:
ProgressDialog progressDialog = null;
// ...
progressDialog = ProgressDialog.show(this, "Please wait...", true);
new Thread() {
public void run() {
try{
// Grab your data
} catch (Exception e) { }
// When grabbing data is finish: Dismiss your Dialog
progressDialog.dismiss();
}
}.start();

Categories

Resources