My fragment is doing more stuff , that's why when I open app comes black screen about 3-5 second and show fragment . How can I do when I click app comes progress dialog download stuff and open fragment? In normal activity I do it with asynctask, But it can't help me in fragment
my code is
in on onActivityCreated GetCategories1.execute();
class GetCategories1 extends AsyncTask {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(add.this);
pDialog.setMessage("Fetching Names...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... arg0) {
do my work
return null;
}
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog.isShowing())
pDialog.dismiss();
populatelistview();
}
}
Related
This question already has answers here:
How to show progress dialog in Android?
(17 answers)
Closed 3 years ago.
I wanted a progress dialog to appear while my activity gets some data. I used Async task but however it doesn't show up
I have tried all the answers of the previous Stack Overflow questions but none of them seem to work for me
private class BackgroundSync extends AsyncTask<Void,Void,Void> {
ProgressDialog progress = new ProgressDialog(MainActivity.this);
#Override
protected void onPreExecute() {
progress.setMessage("Loading");
progress.setTitle("Loading");
progress.show();
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... voids) {
/get data
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
progress.dismiss();
super.onPostExecute();
}
}
I have also tried this using
progress = ProgressDialog.show(MainActivity.this,"Loading","Loading");
as one stack overflow answer suggested but it still doesn't show
I also used the below code, but this time the ProgressDialog doesn't disappear
private class BackgroundSync extends AsyncTask<Void,Void,Void> {
ProgressDialog progress
#Override
protected void onPreExecute() {
progress = new ProgressDialog(MainActivity.this);
progress.show(MainActivity.this,"Loading","Loading");
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... voids) {
SyncEvents();
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
progress.dismiss();
}
}`
What do I do? This ProgressDialog issue has been taking more than 2 hours to solve
use this code
private class BackgroundSync extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
public BackgroundSync(MyMainActivity activity) {
dialog = new ProgressDialog(activity);
}
#Override
protected void onPreExecute() {
dialog.setMessage("Loading");
dialog.setTitle("Loading");
dialog.show();
}
#Override
protected Void doInBackground(Void... args) {
// do background work here
return null;
}
#Override
protected void onPostExecute(Void result) {
// do UI work here
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
I'm trying to display a progress dialog when sign in button is clicked. But the progress dialog is displayed only when i reach the end of my method onClick. And not a the start.
In the signIn method, i do a asynctask to reach the server.
So all the time take by the asynctask, there is no progress dialog displayed. only when the end of the onClick method is reach, the progress dialog is displayed....
Some one know what am i doing wrong?
public void onClick(View view) {
progressDialog = new ProgressDialog(SignIn.this);
progressDialog.setMessage("Sign in in progress");
progressDialog.setTitle("Please wait");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
repSignIn = signIn(SignIn.this, etEmail.getText().toString(), etPassword.getText().toString());
if(!repSignIn.hasError())
{
Toast.makeText(getApplicationContext(), "Sign In successfully", Toast.LENGTH_SHORT).show();
onBackPressed();
}
else
{
Toast.makeText(getApplicationContext(), repSignIn.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
//progressDialog.cancel();
}
Thanks a lot in advance
Nadine
Instead of having progressDialog in onClick you need to put it in onPreExecute of your AsyncTask and dismiss it in onPostExecute.
The reason is AsyncTask uses a worker thread that will be asynchronous and that is the reason why progressDialog finishes before expected.
class MyAsyncTask extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute() {
progressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
// do your stuff
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
progressDialog.dismiss();
}
}
In Async task override onPreExecute() method and there you have to display the progress bar..
private void signIn(your parameters here) {
new AsyncTask<Void, Void, Void>(){
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(SignIn.this);
progressDialog.setMessage("Sign in in progress");
progressDialog.setTitle("Please wait");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.show();
}
#Override
protected void doInBackground(Void... params) {
//your logic here
}
#Override
protected void onPostExecute(JSONObject response) {
super.onPostExecute(response);
progressDialog.dismiss();
}
}.execute();
}
HOpe this helps
Use your progress dailog in Asyntask. Initialise and start your dialog in onPreExecute and dismiss dialog in onPostExecute.
working for me,
#Override
protected void onPreExecute() {
progress = new ProgressDialog(UpdateActivity.this);
progress.setCancelable(false);
progress.setMessage("Please wait data is Processing");
progress.show();
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progress.dismiss();
}
I have change my code
String result = new sendDataToServer().execute(url, "PUT", jsonData.toString()).get();
By doing this:
https://gist.github.com/cesarferreira/ef70baa8d64f9753b4da
And now it's work very well.
Thanks for your help. =)
When I use AsyncTask :
class GetStreetName extends AsyncTask<String, Void, JSONObject>
{
ProgressDialog progressDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity());
progressDialog.setCancelable(true);
progressDialog.setMessage(getString(R.string.dialog_loading));
progressDialog.show();
}
#Override
protected JSONObject doInBackground(String... params) {
// backGround task
}
#Override
protected void onPostExecute(JSONObject result) {
if (result!=null) {
....
progressDialog.dismiss();
enterStreetEditText.requestFocus();
}
}
when I do like this, the keyboard is hiden when the dialog is dismisses in onPostExecute. How can I prevent this and keep showing the keyboard while the dialog is dismissing ?
When I do not using the ProgressDialog, the problem is not occurs.
It may help you:
#Override
protected void onPostExecute(JSONObject result) {
if (result!=null) {
....
progressDialog.dismiss();
enterStreetEditText.setFocusable(true);
enterStreetEditText.requestFocus();
InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(enterStreetEditText , 0);
}
}
I want to implement a progress bar while a function is getting executed.This will help me to notify user that something is going on for updation.
But I am not able to see any progressbar instead I see Logcat message as progressBar dimensions.
class Async extends AsyncTask<Void, Void, String> {
#Override
protected void onPreExecute()
{
ProgressDialog progress=new ProgressDialog(App_list_Activity.this);
progress.setTitle("Please Wait while sync is in progress!!");
progress.setMessage("Database is getting updated...");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
}
#Override
protected String doInBackground(Void... arg0)
{
updateDatabase();
return null;
}
#Override
protected void onPostExecute(String result)
{
ProgressDialog progress=new ProgressDialog(App_list_Activity.this);;
progress.dismiss();
}
}
new Async().execute();
Where am I going wrong?
As the Android documentation states :
onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing. For instance, it can be used to animate a progress bar or show logs in a text field.
Try this:
private ProgressDialog progress;
class Async extends AsyncTask<Void, Void, String> {
#Override
protected void onPreExecute()
{ progress= new ProgressDialog(SingleContactActivity.this);
progress.setTitle("Please Wait while sync is in progress!!");
progress.setMessage("Database is getting updated...");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
}
#Override
protected String doInBackground(Void... arg0)
{
updateDatabase();
return null;
}
#Override
protected void onPostExecute(String result)
{
if (progress.isShowing())
progress.dismiss();
}
}
new Async().execute();
}
Declare ProgressDialog at class level :
class Async extends AsyncTask<Void, Void, String> {
ProgressDialog progress;
Initialize in onPreExecute() :
protected void onPreExecute() {
progress=new ProgressDialog(App_list_Activity.this);
Dismiss in onPostExecute() :
protected void onPostExecute(String result){
progress.dismiss();
In your code you again Initialize ProgressDialog in onPostExecute() for dismiss onPreExecute ProgressDialog so it will create new ProgressDialog instance and dismiss it but still onPreExecute() ProgressDialog is not dismiss.
Example :
class Async extends AsyncTask<Void, Void, String> {
ProgressDialog progress;
#Override
protected void onPreExecute()
{
progress=new ProgressDialog(App_list_Activity.this);
progress.setTitle("Please Wait while sync is in progress!!");
progress.setMessage("Database is getting updated...");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
}
#Override
protected String doInBackground(Void... arg0)
{
updateDatabase();
return null;
}
#Override
protected void onPostExecute(String result)
{
progress.dismiss();
}
}
new Async().execute();
You are creating one instance in onPreExecute and another in onPostExecute. You should keep the reference and dismiss that one.
On top of that I suggest keeping the member progress final, otherwise Android might scream about leak.
Furthermore, dismiss dialog should be in onCancelled as well.
class Async extends AsyncTask<Void, Void, String> {
private final ProgressDialog progress;
public Async(Context c) {
super();
this.progress = new ProgressDialog(c);
progress.setTitle("Please Wait while sync is in progress!!");
progress.setMessage("Database is getting updated...");
progress.setCancelable(true);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
}
#Override
protected void onPreExecute() {
progress.show();
}
#Override
protected String doInBackground(Void... arg0) {
updateDatabase();
return null;
}
#Override
protected void onPostExecute(String result) {
if (progress.isShowing()) {
progress.dismiss();
}
super.onPostExecute(result);
}
#Override
protected void onCancelled() {
if (progress.isShowing()) {
progress.dismiss();
}
super.onCancelled();
}
}
new Async().execute();
HTH.
I want to show ProgressDialog when I click on Login button and it takes time to move to another page. How can I do this?
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.setMessage("loading");
pd.show();
And that's all you need.
You better try with AsyncTask
Sample code -
private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
public YourAsyncTask(MyMainActivity activity) {
dialog = new ProgressDialog(activity);
}
#Override
protected void onPreExecute() {
dialog.setMessage("Doing something, please wait.");
dialog.show();
}
#Override
protected Void doInBackground(Void... args) {
// do background work here
return null;
}
#Override
protected void onPostExecute(Void result) {
// do UI work here
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
Use the above code in your Login Button Activity. And, do the stuff in doInBackground and onPostExecute
Update:
ProgressDialog is integrated with AsyncTask as you said your task takes time for processing.
Update:
ProgressDialog class was deprecated as of API 26
To use ProgressDialog use the below code
ProgressDialog progressdialog = new ProgressDialog(getApplicationContext());
progressdialog.setMessage("Please Wait....");
To start the ProgressDialog use
progressdialog.show();
progressdialog.setCancelable(false); is used so that ProgressDialog cannot be cancelled until the work is done.
To stop the ProgressDialog use this code (when your work is finished):
progressdialog.dismiss();`
Point one 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 a 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.
Simple coding in your activity like below:
private ProgressDialog dialog = new ProgressDialog(YourActivity.this);
dialog.setMessage("please wait...");
dialog.show();
dialog.dismiss();
Declare your progress dialog:
ProgressDialog progressDialog;
To start the progress dialog:
progressDialog = ProgressDialog.show(this, "","Please Wait...", true);
To dismiss the Progress Dialog :
progressDialog.dismiss();
ProgressDialog is now officially deprecated in Android O. I use DelayedProgressDialog from https://github.com/Q115/DelayedProgressDialog to get the job done.
Usage:
DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");
This is the good way to use dialog
private class YourAsyncTask extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog = new ProgressDialog(IncidentFormActivity.this);
#Override
protected void onPreExecute() {
//set message of the dialog
dialog.setMessage("Loading...");
//show dialog
dialog.show();
super.onPreExecute();
}
protected Void doInBackground(Void... args) {
// do background work here
return null;
}
protected void onPostExecute(Void result) {
// do UI work here
if(dialog != null && dialog.isShowing()){
dialog.dismiss()
}
}
}
when you call in oncreate()
new LoginAsyncTask ().execute();
Here how to use in flow..
ProgressDialog progressDialog;
private class LoginAsyncTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
progressDialog= new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Please wait...");
progressDialog.show();
super.onPreExecute();
}
protected Void doInBackground(Void... args) {
// Parsse response data
return null;
}
protected void onPostExecute(Void result) {
if (progressDialog.isShowing())
progressDialog.dismiss();
//move activity
super.onPostExecute(result);
}
}
ProgressDialog dialog =
ProgressDialog.show(yourActivity.this, "", "Please Wait...");
ProgressDialog pd = new ProgressDialog(yourActivity.this);
pd.show();
Step 1:Creata a XML File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/btnProgress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Progress Dialog"/>
</LinearLayout>
Step 2:Create a SampleActivity.java
package com.scancode.acutesoft.telephonymanagerapp;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class SampleActivity extends Activity implements View.OnClickListener {
Button btnProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnProgress = (Button) findViewById(R.id.btnProgress);
btnProgress.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final ProgressDialog progressDialog = new ProgressDialog(SampleActivity.this);
progressDialog.setMessage("Please wait data is Processing");
progressDialog.show();
// After 2 Seconds i dismiss progress Dialog
new Thread(){
#Override
public void run() {
super.run();
try {
Thread.sleep(2000);
if (progressDialog.isShowing())
progressDialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
}
final ProgressDialog loadingDialog = ProgressDialog.show(context,
"Fetching BloodBank List","Please wait...",false,false); // for showing the
// dialog where context is the current context, next field is title followed by
// message to be shown to the user and in the end intermediate field
loadingDialog.dismiss();// for dismissing the dialog
for more info
Android - What is difference between progressDialog.show() and ProgressDialog.show()?
ProgressDialog is deprecated since API 26
still you can use this:
public void button_click(View view)
{
final ProgressDialog progressDialog = ProgressDialog.show(Login.this,"Please Wait","Processing...",true);
}
Simple Way :
ProgressDialog pDialog = new ProgressDialog(MainActivity.this); //Your Activity.this
pDialog.setMessage("Loading...!");
pDialog.setCancelable(false);
pDialog.show();
final ProgressDialog progDailog = ProgressDialog.show(Inishlog.this, contentTitle, "even geduld aub....", true);//please wait....
final Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
Barcode_edit.setText("");
showAlert("Product detail saved.");
}
};
new Thread() {
public void run() {
try {
} catch (Exception e) {
}
handler.sendEmptyMessage(0);
progDailog.dismiss();
}
}.start();
Whenever you want ProgressDialog call this method
private void startLoader() {
progress = new ProgressDialog(this); //ProgressDialog
progress.setTitle("Loading");
progress.setMessage("Please wait");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setCancelable(false);
progress.show();
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(7000);
} catch (Exception e) {
e.printStackTrace();
}
progress.dismiss();
}
}).start();
}