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.
Related
I have a ProgressDialog running in a AsyncTask. I want to change the template progressDialog in AsyncTask for android.
Can somebody help me please, is this possible? Thank you in advance.
class LoadData extends AsyncTask<Void, Void, Void>
{
private ProgressDialog prgDialog;
#Override
protected void onPreExecute()
{
prgDialog = new ProgressDialog(DataActivity.this);
prgDialog.setMessage("Please wait...");
prgDialog.setCancelable(false);
prgDialog.show();
super.onPreExecute();
}
#Override
protected Void doInBackground(Void... params)
{
// do some work like loading data
return null;
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
prgDialog.cancel();
// do some work when loading finish
}
for example:
loading
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'm trying to show a progress dialog while the twitter feed is loading up...However the progress dialog remains on screen when the twitter feed appears. Any help is much appreciated.
public class MainActivity extends ListActivity {
final static String twitterScreenName = "CFABUK";
final static String TAG = "MainActivity";
private AsyncTask<Object, Void, ArrayList<TwitterTweet>> tat;
boolean done;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
done=false;
AndroidNetworkUtility androidNetworkUtility = new AndroidNetworkUtility();
if (androidNetworkUtility.isConnected(this)) {
TwitterAsyncTask syn=new TwitterAsyncTask();
syn.execute(twitterScreenName,this);
ProgressDialog pd = new ProgressDialog(MainActivity.this);
pd.setMessage("loading");
pd.show();
do {
if(!(syn.getStatus()==AsyncTask.Status.RUNNING)) {
pd.dismiss();
pd.cancel();
done=true;
}
} while(done=false);
} else {
Log.v(TAG, "Network not Available!");
}
}
}
You must call ProgressDialog show() method on AsyncTasks onPreExecute(). For example:
class MyTask extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
#Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("loading");
pd.show();
}
#Override
protected Void doInBackground(Void... params) {
// Do your request
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pd != null)
{
pd.dismiss();
}
}
}
You must use a onPreExecute and onPostExecute of AsyncTask class. For example:
class AsyncData extends AsyncTask<Void, Void, Void>{
#Override
protected void onPreExecute() {
super.onPreExecute();
// init progressdialog
}
#Override
protected Void doInBackground(Void... arg0) {
// get data
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// dismiss dialog
}
}
The methods onPreExecute(), doInBackground() and onPostExecute() of AsyncTask are used for purpose that you mentioned -
public class MainActivity extends ListActivity {
final static String twitterScreenName = "CFABUK";
final static String TAG = "MainActivity";
private AsyncTask<Object, Void, ArrayList<TwitterTweet>> tat;
boolean done;
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
done=false;
context = this;
new NetworkTask().execute();
}
}
class NetworkTask extends AsyncTask<String, String, String>
{
Context ctx = context ;
#Override
protected void onPreExecute()
{
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Working ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected String doInBackground(String... args)
{
//Do your background work here and pass the value to onPostExecute
AndroidNetworkUtility androidNetworkUtility = new AndroidNetworkUtility();
if (androidNetworkUtility.isConnected(ctx)) {
TwitterAsyncTask syn=new TwitterAsyncTask();
syn.execute(twitterScreenName,this);
while(done)
{
if(!(syn.getStatus()==AsyncTask.Status.RUNNING))
{
done=true;
}
else
{
Log.v(TAG, "Network not Available!");
}
}
return done + "";
}
protected void onPostExecute(String result)
{
//Do something with result and close the progress dialog
pDialog.dismiss();
}
ProgressBar is best alternative for ProgressDialog. A user interface element that indicates the progress of an operation.
ProgressDialog is deprecated in latest versions.
For more info see android developer official site: https://developer.android.com/reference/android/widget/ProgressBar.html
I have a time consuming task that is taking place in the doInBackground() method, but from within that method, I want to be able to update the progressDialog with percentage data. How can I do this?
class GetDataTask extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute()
{
mProgressDialog = ProgressDialog.show(getActivity(),Constants.APP_NAME,"Getting data", true);
}
#Override
protected Void doInBackground(Void... params)
{
//go get data, update mProgressDialog
return null;
}
#Override
protected void onPostExecute(Void res)
{
}
}
Override the onProgressUpdate() method like this:
#Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setProgress(progress[0]);
}
and publish the progress with
publishProgress((int) (total * 100 / fileLength));
Try to refer to this link
AsyncTask has method onProgressUpdate(Integer...) that you can call each iteration for example or each time a progress is done during doInBackground() by calling publishProgress().
Refer to the docs for more details
Note that the second parameter is the progress variable
class GetDataTask extends AsyncTask<Void, String, Void> {
#Override
protected void onPreExecute()
{
mProgressDialog = ProgressDialog.show(getActivity(),Constants.APP_NAME,"Getting data", true);
}
#Override
protected Void doInBackground(Void... params)
{
publishProgress(""+(int)count);
return null;
}
protected void onProgressUpdate(String... progress) {
/**
* Sets current progress on ProgressDialog
*/
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
#Override
protected void onPostExecute(Void res)
{
}
}