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;
}
}
Related
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.
I am trying to cancel a dialog from the mainthread while the 'doInBackGround' method of AsyncTask is running. While I am downloading a photo, a progress dialog pops up and when it is finished downloading I dismis() the dialog in onPostExecute. If the connection is slow, the dialog is up for a while and I cannot cancel it until there is a timeout error or it finishes downloading. How do I use the back-button so the main thread can access. Here is what my code looks like:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
protected void onPreExecute() {
//this piece code doesn't seem to work
progressDialog = ProgressDialog.show(context, "",
"Image loading", true);
}
protected Bitmap doInBackground(String... urls) {
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
//bmImage.setImageBitmap(result);
progressDialog.dismiss();
someMethod(result);
}
}
Use a cancellable progress dialog, pass in a cancel listener to the progress dialog and cancel the task within that method, eg
protected void onPreExecute() {
progressDialog = ProgressDialog.show(activity, "Searching files", "Scanning...", true, true,
new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// When dialog in cancelled, need to explicitly cancel task otherwise it keeps on running
cancel(true);
}
}
);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}
You can intercept the onBackKeyPressed event, and cancel the task using cancel method.
See that link:
Ideal way to cancel an executing AsyncTask
You can try to use a ProgressDialog which is cancelable. This is the signature of the method:-
public static ProgressDialog show (Context context, CharSequence title, CharSequence message, boolean indeterminate, boolean cancelable)
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();
I have an alert dialog box in my application for login authentication. While sending the request i want to show a progress bar and want to dismiss if the response is success.please help me if anyone knows.Iam using the below code:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
LinearLayout login = new LinearLayout(this);
TextView tvUserName = new TextView(this);
TextView tvPassword = new TextView(this);
TextView tvURL = new TextView(this);
final EditText etUserName = new EditText(this);
final EditText etPassword = new EditText(this);
final EditText etURL = new EditText(this);
login.setOrientation(1); // 1 is for vertical orientation
tvUserName.setText(getResources().getString(R.string.username));
tvPassword.setText(getResources().getString(R.string.password));
tvURL.setText("SiteURL");
login.addView(tvURL);
login.addView(etURL);
login.addView(tvUserName);
login.addView(etUserName);
login.addView(tvPassword);
etPassword.setInputType(InputType.TYPE_CLASS_TEXT
| InputType.TYPE_TEXT_VARIATION_PASSWORD);
login.addView(etPassword);
alert.setView(login);
alert.setTitle(getResources().getString(R.string.login));
alert.setCancelable(true);
alert.setPositiveButton(getResources().getString(R.string.login),
new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog,
int whichButton) {
strhwdXml = etURL.getText().toString();
strUserName = etUserName.getText().toString();
XmlUtil.username = strUserName;
strPassword = etPassword.getText().toString();
if ((strUserName.length() == 0)
&& (strPassword.length() == 0)
&& (strhwdXml.length() == 0)) {
Toast.makeText(
getBaseContext(),
getResources().getString(
R.string.userPassword),
Toast.LENGTH_SHORT).show();
onStart();
} else {
final SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor prefsEditor = prefs
.edit();
try {
StringBuffer inStreamBuf = new StringBuffer();
inStreamBuf = XmlUtil
.getLoginAuthResponse(strUserName,
strPassword, strhwdXml);
strXmlResponse = inStreamBuf.toString();
Log.e("Response:", strXmlResponse);
String parsedXML = ParseResponse(strXmlResponse);
if (parsedXML
.equalsIgnoreCase(getResources()
.getString(R.string.success))) {
}
It might be easier to use this
ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true);
You can read more about progress dialogs here
To cancel would be
dialog.dismiss();
This class was deprecated in API level 26. ProgressDialog is a modal
dialog, which prevents the user from interacting with the app. Instead
of using this class, you should use a progress indicator like
ProgressBar, which can be embedded in your app's UI. Alternatively,
you can use a notification to inform the user of the task's progress.For more details Click Here
Since the ProgressDialog class is deprecated, here is a simple way to display ProgressBar in AlertDialog:
Add fields in your Activity:
AlertDialog.Builder builder;
AlertDialog progressDialog;
Add getDialogProgressBar() method in your Activity:
public AlertDialog.Builder getDialogProgressBar() {
if (builder == null) {
builder = new AlertDialog.Builder(this);
builder.setTitle("Loading...");
final ProgressBar progressBar = new ProgressBar(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
progressBar.setLayoutParams(lp);
builder.setView(progressBar);
}
return builder;
}
Initialize progressDialog:
progressDialog = getDialogProgressBar().create();
Show/Hide AlertDialog whenever u want using utility methods:
progressDialog.show() and progressDialog.dismiss()
If you want the progress bar to show, try the following steps and also you can copy and paste the entire code the relevant portion of your code and it should work.
//the first thing you need to to is to initialize the progressDialog Class like this
final ProgressDialog progressBarDialog= new ProgressDialog(this);
//set the icon, title and progress style..
progressBarDialog.setIcon(R.drawable.ic_launcher);
progressBarDialog.setTitle("Showing progress...");
progressBarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//setting the OK Button
progressBarDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,
int whichButton){
Toast.makeText(getBaseContext(),
"OK clicked!", Toast.LENGTH_SHORT).show();
}
});
//set the Cancel button
progressBarDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton){
Toast.makeText(getApplicationContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
}
});
//initialize the dialog..
progressBarDialog.setProgress(0);
//setup a thread for long running processes
new Thread(new Runnable(){
public void run(){
for (int i=0; i<=15; i++){
try{
Thread.sleep(1000);
progressBarDialog.incrementProgressBy((int)(5));
}
catch(InterruptedException e){
e.printStackTrace();
}
}
//dismiss the dialog
progressBarDialog.dismiss();
}
});
//show the dialog
progressBarDialog.show();
The cancel button should dismiss the dialog.
Try below code
private class DownloadingProgressTask extends
AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog = new ProgressDialog(ShowDescription.this);
/** progress dialog to show user that the backup is processing. */
/** application context. */
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
protected Boolean doInBackground(final String... args) {
try {
// write your request code here
**StringBuffer inStreamBuf = new StringBuffer();
inStreamBuf = XmlUtil
.getLoginAuthResponse(strUserName,
strPassword, strhwdXml);
strXmlResponse = inStreamBuf.toString();
Log.e("Response:", strXmlResponse);
String parsedXML = ParseResponse(strXmlResponse);
if (parsedXML
.equalsIgnoreCase(getResources()
.getString(R.string.success))) {**
return true;
} catch (Exception e) {
Log.e("tag", "error", e);
return false;
}
}
#Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (success) {
Toast.makeText(ShowDescription.this,
"File successfully downloaded", Toast.LENGTH_LONG)
.show();
imgDownload.setVisibility(8);
} else {
Toast.makeText(ShowDescription.this, "Error", Toast.LENGTH_LONG)
.show();
}
}
}
and call this in onclick event
new DownloadingProgressTask().execute();
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();