Android ProgressDialog "Spinner" disappear(doesn't show up) - android

I'm working on an android app and I need to fetch data from internet.
I declare an "private Progress dialog" in Homepage.java.
In onCreate() method, call the function
dialog = ProgressDialog.show(Homepage.this, "Connecting", "Please wait for a while...", true);
And create another thread to fetch the data, and in finally block, call
"dialog.dismiss()"
The whole onResume() code is shown in below.
#Override
protected void onResume(){
super.onResume();
Log.e(TAG, "onResume");
dialog = ProgressDialog.show(Homepage.this, "Connecting", "Please wait for a while..", true);
/******Start fetching data.******/
Thread getDataThread = new Thread() {
#Override
public void run() {
try {
try {
getData();
} catch (JSONException e) {
alert_error();
e.printStackTrace();
} catch (IOException e) {
alert_error();
e.printStackTrace();
} finally {
dialog.dismiss();
}
} catch (SQLException e) {
alert_error();
e.printStackTrace();
} catch (ExecutionException e) {
alert_error();
e.printStackTrace();
} catch (InterruptedException e) {
alert_error();
e.printStackTrace();
}finally{
dialog.dismiss();
}
}
};
getDataThread.start();
}
At first, this worked great.(the spinner showed)
But at some point(I don't remember when), the dialog still works, but the spinner disappear
Show as below(place marked with red line should be the place where spinner display)
Spinner is gone
The dialog still works, but all ProgressDialog called anywhere from this app, the spinner is gone.(not even show up)
If I changed the getting data from internet part to sleeping for 10 secs
Which is shown below
#Override
protected void onResume(){
super.onResume();
Log.e(TAG, "onResume");
dialog = ProgressDialog.show(Homepage.this, "Connecting", "Please wait for a while...", true);
/******Start fetching data.******/
Thread getDataThread = new Thread() {
#Override
public void run() {
try {
sleep(10*1000);
} catch (InterruptedException e) {
e.printStackTrace();
dialog.dismiss;
}
}
};
getDataThread.start();
}
The spinner still doesn't show up.
I think that might be something related to global settings or values in my project, I create a empty activity with only one method other than super.onCreate and setContentView
I called
"ProgressDialog dialog = ProgressDialog.show(Homepage.this, "123", "456", true);"
and the spinner still doesn't show up!
But if I create a new project and added the same code, it works!
Thanks in advance!

I think the problem is in your phone setting, I had the same issue once, what you need to do is:
Go to developer options in your phone setting.
Turn transition on (in my case i had them turned off to make the phone act fast)
Restart the app and hopefully spinner will be there.
UPDATE:
Or you can try a library like Material Dialogs for that purpose, its one of my favorites, all you need to do is
Compile
compile 'com.afollestad.material-dialogs:core:0.8.6.0'
and then where you want to show the dialog
new MaterialDialog.Builder(this)
.title(R.string.progress_dialog)
.content(R.string.please_wait)
.progress(true, 0)
.show();
you can find more info here to customize it further and usage details.
Hope it helps

Related

Using AsyncTask in AlertDialog brings out lagging UX?

I know this is a kind of weird title to describe the question. I have a code as below:
new AlertDialog.Builder(getActivity()).setView(myScrollView)
.setPositiveButton("Sent Query", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
try {
JSONStringURL = myURL_here;
image = new AsyncTaskParseJson1(JSONStringURL).execute().get();
if (image.size() == 0) {
Toast.makeText(getActivity(), "no data", Toast.LENGTH_LONG).show();
} else {
overlayImage(image);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}.show();
when i press "Positive Button", the app should cancel the AlertDialog immediately, and then execute the AsyncTask. Unfortunately, the AlertDialog will lag for 3~4 sec and then run the AsyncTask makes the UX feel really bad, but i have no idea why it will lag?
NEVER use AsyncTask.get(). That makes you wait for the task to finish, which totally ruins the idea of it being asynchronous. The fact the function even exists is a design flaw in Android. Instead, anything that needs to be done after the AsyncTask finishes should be done in onPostExecute() of the task (or in a function called from there).
Obviously this is your problem- you aren't closing the dialog and doing the task in parallel, you're waiting for the task to finish (remember the UI will not update until control is returned to the UI thread looper).
You have to use these changes
dialog.dismiss();
In place of
dialog.cancel();

Progress Dialog in OnCompletion method

I have following code in which Progress Dialog do not shows.
#Override
public void onCompletion(MediaPlayer arg0) {
TrackAnalysis a = null;
File file = new File(songs.get(index).path);
final Track track;
try {
track = echoNest.uploadTrack(file, true);
final ProgressDialog dialog2 = ProgressDialog.show(MainGameActivity.this, "Analyzing...", "Please wait...",
true);
dialog2.setCancelable(true);
new Thread(new Runnable(){
public void run(){
try{
Thread.sleep(3000);
}
catch (Exception e){
e.printStackTrace();
}
dialog2.dismiss();
}
}).start();
track.waitForAnalysis(30000);
a = track.getAnalysis();
Log.i("TUTAJ", a.getTempo().toString() + track.getArtistName() + track.getTitle());
} catch (EchoNestException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
OnCompletion method is from overloaded MediaPlayer.OnCompletionListener.
My question is why progress bar do not shows? When I placed ProgressBar code to onClicked method, then it works fine.
Check if you are calling start on media player before you set onCompletionlistener.
on clicked method does not require media player to start before its set.
You need to run the UI related code on main thread. You could either use runOnUiThread or make use of Handler. There are many examples available in the API Guides section of Android Developers site. Please take a look at Processes and Threads for a start.

Android exit with toast

I have a quit() method that I am using. I use a toast to print out "thank you for using this app". On the next line I do a system.exit(0); and when I run the app, the toast doesn't show before the system exits the app. Is there a way to fix this?
The way you should do it, in your activity:
Toast.makeText(this,"Thanks for using the app",Toast.LENGTH_SHORT).show();
finish();
Forget System.exit
You could run a thread that waits for the duration of your Toast.-
Thread thread = new Thread(){
#Override
public void run() {
try {
Thread.sleep(WAIT_TIME_IN_MILLIS);
} catch (Exception e) {
e.printStackTrace();
}
}
};
thread.start();

Logout functionality for facebook in android app

I am implementing following simple functionality.
I have a simple button, clicking on which I am able to login to Facebook. I am using Facebook SDK for the same. When I click , the src image of the button(imageview) also gets updated.
Up to this point everything works fine. But when i click on the same button for logging out
I get a
android.os.networkonmainthreadexception
exception.
Can anyone please help me solve this issue?
EDIT:
my code is as follows:
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.fb_button:
try{
if(fb.isSessionValid())
{
try {
fb.logout(getBaseContext());
update_fb_buttonimage();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//button close session
}
else
{
fb.authorize(LoginPage.this, new DialogListener(){
#Override
public void onFacebookError(FacebookError e)
{
Toast.makeText(LoginPage.this, "on Facebook error", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(DialogError e)
{
Toast.makeText(LoginPage.this, "on error", Toast.LENGTH_SHORT).show();
}
#Override
public void onComplete(Bundle values)
{
update_fb_buttonimage();
Toast.makeText(getBaseContext(), "onComplete works",Toast.LENGTH_LONG).show();
}
#Override
public void onCancel()
{
}
});
//login in to facebook
}
}catch(Exception e){
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
thankyou!
The problem here is simply that you need to make your web service calls (or what-have-you) on a separate thread. So, quite simply, you’ll need to look into how to do threading with Android. Unfortunately this can be a bit of a pain because you need to make your service calls on a separate thread, but you need to update the UI on the main thread. Normally this would require passing data between the threads, which involves handlers or other complexities. Luckily the Android platform provides the Async Task to handle this, which alleviates some of this complexity and may help you avoid some clutter in your code.
This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask

Cancelling an AsyncTask stops starting method

public void get(View view){
try {
asPt = new ProgressTask().execute(null,null,null);
Log.d("Watcher","Get finished");
}
catch (Exception e) {
e.printStackTrace();
Log.e("Watcher","Get Exception");
}
}
When I cancel(Boolean) the AsyncTask asPt the Line "Get finished" is never printed.
Why? It also doesn't catch an Exception in this method.
Remember cancel does nothing so you need to implement it yourself, see link: Android - Cancel AsyncTask Forcefully

Categories

Resources