Progress Dialog not showing up was solved by removing the blocking call.
My purpose is to download large amount of data from internet and keep the end user informed about download status, however I have to wait to the data to complete downloading for proceeding to the next step, and because of that I have to block the code from executing.
Blocking causes the progress dialog not to show up or freeze. I need tip for implementing those tasks the best way. Because my tasks are simple it seems for me to be overkill to implement task complete listener and I was wondering if there was any other way to solve my problem?
You can use this:
#Override
protected void onPreExecute() {
dialog= ProgressDialog.show(YourActivity.this, "", "MessageYouWantToDisplay");
}
#Override
protected void onPostExecute(T result) {
dialog.dismiss();
}
You call the class that extends AsycTask by typing:
new NameOFClass().execute();
Try this first as a test and put a Thread.sleep() in you doInBackground method to understand how it works. And after that use it in your project with your true methods, data etc.
Try this:::
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(YourActivity.this);
dialog.setMessage("Progress start");
dialog.show();
Log.d(TAG, "Showing dialog");
}
#Override
protected void onPostExecute(T result) {
dialog.dismiss();
Log.d(TAG, "Dismissing dialog");
}
Related
I have an activity that calls a second java class. I want after the second class is called to show a progressbar and then return to normal activity execution. I found some other threads but i couldn't make the progressbar to stop.
There's a full example over here.
Quote:
Declare your progress dialog:
ProgressDialog progress;
When you're ready to start the progress dialog:
progress = ProgressDialog.show(this, "dialog title",
"dialog message", true);
and to make it go away when you're done:
progress.dismiss();
Here's a little thread example for you:
// Note: declare ProgressDialog progress as a field in your class.
progress = ProgressDialog.show(this, "dialog title",
"dialog message", true);
new Thread(new Runnable() {
#Override
public void run()
{
// do the thing that takes a long time
runOnUiThread(new Runnable() {
#Override
public void run()
{
progress.dismiss();
}
});
}
}).start();
ProgressDialog is deprecated, so you might want to use a ProgressBar.
I've found this post about deleting one of them.
Well, I think this is rather ridiculous, but here is how I fixed it.
In my xml for the ProgressBar, I added android:visibility="gone"
to hide it by default. Then, in my code, I first told it to display
(View.VISIBLE) before it tried getting the server list, then I told
it to hide (View.GONE) after it was done. This worked (I could see
the progress indicator while the data loaded, then it went away). So
I suppose I couldn't get it to hide in the code because the code is
not what forced it to be visible to begin with... That seems like a
bug to me.
Its very Simple:
to show a Progress
ProgressDialog dialog = ProgressDialog.show(getContext(), "Title", "Message");
and to stop it:
dialog.dismiss();
I was wondering if I can display some kind of animation until my activity loads. Because when I start my app , it takes about 5 seconds to see the Activity. Until it loads, a white screen is displayed (refer to the image below).
Can I do that ?
And if I can , how it should be done ?
Thanks you in advance.
Have a great day.
if you are downloading some data or u got ridiculusly large listview - you got other problems....
downloading data: reduce amout of data that is needed for app to work
listview - user freaking recyclerview
and if this is a legit problem - then you need some sort of listener(that indicates whenever job is complete) like Thread.join() and whenever that listener activates - it should go to another intent and kill last one (to kill you need to use finish();)
Try to use ProgressDialog in AsyncTask
new YourFragment.YourAsyncTask().execute();
Use this library sweet-alert-dialog for more animation Loading
class YourAsyncTask extends AsyncTask<Void, Void, Void> {
SweetAlertDialog pDialog;
#Override
protected void onPreExecute() {
//show your dialog here
super.onPreExecute();
pDialog = new SweetAlertDialog(getContext(), SweetAlertDialog.PROGRESS_TYPE);
pDialog.getProgressHelper().setBarColor(Color.parseColor("#fdc80b"));
pDialog.setTitleText("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Void result) {
//hide your dialog here
super.onPostExecute(result);
pDialog.dismissWithAnimation();
}
}
}
}
I have an app that user submit the log in form , when it sent the data to server and create a connection for its account.
In this connection i have an integer field named as state.
the state value is : 1 for connecting, 2 for connected and 0 for failed.
I want to show a dialog to user show is Connecting ... and then check the state of connection if its return 0 or 2 dismiss the dialog and show the related message else if it doesn't change after 15 sec dismiss dialog and change the state to 0 !
How can i do this logic ?
I am assuming to make the network all you are using an Asynctask. In this case you can use the methods onPreExecute and onPostExecute.
For more information about network calls and Asynctasks, please read http://developer.android.com/training/basics/network-ops/connecting.html. I've given a brief explanation below though.
If you create a dialog or initialise it in your onCreate method (or something similar), you can call the below methods to show and hide the dialog when the call starts and finishes
onPreExecute() {
dialog.show();
}
onPostExecute(Object result) {
dialog.dismiss();
}
You can also modify the UI from doInBackground through the use of onProgressUpdate(). This will allow you to call to the dialog whilst performing the logic in doInBackground by calling the method publishProgress(). The exact place you should call the method I'm not sure of because I don't fully understand your bigger picture but I hope this helps you along the way
This is one way.You could also use AsyncTask.onCancelled()
public class TestActivity extends Activity{
private Dialog dialog;
#override
protected void onCreate(Bundle bundle){
//relevant activity code.
TestAsync ta=new TestAsync().execute();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
ta.cancel();
if(dialog!=null)
dialog.dismiss();
}
}15*1000);
}
public class TestAsync extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
//create your dialog here
}
#Override
protected void onPostExecute(Void result) {
if(dialog!=null){
dialog.dismiss();
}
}
#Override
protected Void doInBackground(Void... params) {
//relevant AsyncTask code
}
}
}
In my project I have a lot of asynctask, which all follow this pattern:
#Override
protected void onPreExecute() {
super.onPreExecute();
crossfade(progressBar, contentView);//hide content, show progress bar
}
#Override
protected Void doInBackground(String... arg0) {
//some work
}
protected void onPostExecute(Void unused) {
crossfade(contentView, progressBar);
}
Code for crossfade:
void crossfade(View contentView, View loadingView){
Runnable r = new Runnable(){
#Override
public void run() {
if(contentView != null){
contentView.setAlpha(0f);
contentView.setVisibility(View.VISIBLE);
contentView.animate()
.alpha(1f)
.setDuration(CROSSFADE_TIME)
.setListener(null);
}
if(loadingView != null){
loadingView.animate()
.alpha(0f)
.setDuration(CROSSFADE_TIME)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
if(loadingView != null){
loadingView.setVisibility(View.GONE);
}
}
});
}
}
};
runOnUiThread(r);
}
The problem happens when asynctask executes faster than animation time, causing second crossfade call before the first one is finished, resulting in both views being invisible.
I tried queueing runnables to execute them sequentially, but the problem is if user clicks a lot of buttons or many fragments are being loaded(they use crossfade method too), UI thread becomes overloaded and it may crash my app. The only solution I see so far is to add extra delay to all my asynctasks, using Thread.sleep(CROSSFADE_TIME), however it looks like a really dirty hack and I'm not sure if it's a good user experience.
In case someone needs it in future, adding loadingView.animate().cancel() and contentView.animate().cancel() before animation cancels previous animations and everything works ok.
Why are you running the crossfade animation in a separate thread? Try to do the animation without
Runnable r = new Runnable(){ and runOnUiThread(r);
I'm working on an app that connect to a webpage to get some content. I want to show a progressdialog, but I think I'm doing something wrong.
This is my code:
final ProgressDialog myProgressDialog = ProgressDialog.show(WhoisBeyondActivity.this, "Wait...", "Fetching data...", true);
Handler handler=new Handler();
handler.post(new Runnable()
{
public void run()
{
try {
// code to execute
Thread.sleep(2000);
} catch (Exception e) {
}
myProgressDialog.dismiss();
}
});
The problem is that the progressdialog is only shown one second at the end of the operation I want to make. I think the progressdialog is only executing when I execute the dismiss() because it appears and dissapears quickly. Is like the progressdialog appears only to dissapear ... help me please!!! I have read a lot of tutorials, and I have try a lot of option, like THREAD instead of HANDLER, but it is not usefull for me, because I have to edit UI.
There's an excellent example and tutorial here:
http://www.helloandroid.com/tutorials/using-threads-and-progressdialog
That's what I used the first time I did a threaded dialog in Android, and I bookmarked it. Hopefully it helps.
The reason you are getting the described behaviour is that the post method will just execute the passed in runnable against the thread to which the Handler is attached. In your case this is the UI thread.
You are calling ProgressDialog.show(), which is asynchronous. This does not actually show the dialog as soon as the method returns, rather you have just requested that the UI display a dialog. You then immediately post a thread that sleeps for 2 seconds, which is added to the UI queue and blocks the UI from performing the dialog show. The UI then wakes from your sleep, shows the dialog then is dismissed.
You should perform any network operation in either a new Thread or in an AsyncTask. Have a look at these links for more details:
AsyncTask
Painless threading
Threading
Designing for responsiveness
Thread documentation
Handler documentation
You don't want to use a separate thread per-say. What you really want is an AsynTask. This will allow you to create the progress dialog and do the background processing right there in the task. Simple to write and easier to implement. If your refer to the link, what you need is actually really similar to your question. With a little tweaking, it should work just fine for you.
public class HelloActivity extends Activity {
protected static final String TAG = "HelloActivity";
ProgressDialog myProgressDialog;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
showDialog(1);
final Handler handler=new Handler(){
#Override
public void handleMessage(Message msg) {
//update UI here depending on what message is received.
switch(msg.what){
case 0:
myProgressDialog.dismiss();
}
super.handleMessage(msg);
}
};
Thread t = new Thread(){
public void run(){
try {
// code to execute
Thread.sleep(5000);
} catch (Exception e) {
}
handler.sendEmptyMessage(0);//nothing to send
}
};
t.start();
}
#Override
protected Dialog onCreateDialog(int id) {
myProgressDialog = ProgressDialog.show(HelloActivity.this, "Wait...", "Fetching data...", true);
return myProgressDialog;
}
}