Progress Dialog spinner is not spinning - android

I am creating one Progress dialog in my app. Below is my code...
ProgressDialog dialog = ProgressDialog.show(TabViewLayout.this, "", "Logout...");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.isIndeterminate();
new Thread() {
public void run() {
try {
sleep(5000);
}
catch (Exception e) { }
}
}.start();
Handler handler = new Handler();
handler.post(new Runnable(){
public void run(){
dialog.dismiss();
}
});
What I have to do is, I have to put delay for 5 seconds in my app for some process. With above code, progress dialog is appearing on screen but spinner is not spinning. I want it in spinning mode for 5 seconds. What needs to be done?

Use this code it will work for you
ProgressDialog progressDialog;
Context mContext;
private Thread mThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
progressDialog = new ProgressDialog(mContext);
progressDialog.setCancelable(false);
progressDialog.setMessage("Loading....");
progressDialog.show();
mThread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
// Wait given period of time or exit on touch
wait(5000);
}
} catch (InterruptedException ex) {
}
// finish();
progressDialog.dismiss();
}
};
mThread.start();
}

try this code
ProgressDialog dialog = ProgressDialog.show(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
new Thread() {
public void run() {
try {
sleep(5000);
}
catch (Exception e) { }
}
}.start();
Handler handler = new Handler();
handler.post(new Runnable() {
public void run() {
dialog.dismiss();
}
});

Related

How to pass variables to UI thread from worker thread?

Below is the code for simple get request and res variable is not available inside Ui thread. How this can be achieved in android?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread thread = new Thread(new Runnable(){
#Override
public void run(){
try {
String res = Utils.GetRequest("http://www.google.com");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this, res, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
Okay, I found out that I haven't declared the res variable as final.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread thread = new Thread(new Runnable(){
#Override
public void run(){
try {
final String res = Utils.GetRequest("http://www.google.com");
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(MainActivity.this, res, Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();
}
You have to create a handler on the Main thread in the OnCreate()
Handler mHandler = new Handler(Looper.getMainLooper()) {
#Override
public void handleMessage(Message inputMessage) {
// handle the passed value here
// for ex. update the UI by getting the data from the inputMessage
}
}
inside your Thread .. call the
Message myMessage = mHandler.obtainMessage();
myMessage.obj = "the value to update the ui";
mHandler.sendMessage(myMessage);

ProgressBar is not set visible before other operations by click button

I work with MediaPlayer and want to show ProgressBar before calling method mediaPlayer.prepare() (it can take up some time). I tried AsynkTast, Thread, Handler but it is show progressBar only after all operations at the end of clickPlayButton method. What I do wrong?
Thanks!
ProgressBar declaration:
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"/>
clickPlayButton method
public void clickPlayButton(View view) {
Thread t = new Thread(new Runnable() {
public void run() {
h.post(updateProgress);
}
});
t.start();
// progressBar.setVisibility(ProgressBar.VISIBLE);
// new ProgressTask().execute();
mTimer = new Timer();
mMyTimerTask = new MyTimerTask();
if(!mediaPlayer.isPlaying()){
btnPlay.setImageResource(R.drawable.player_stop);
try {
mediaPlayer.setDataSource(DATA_HTTP);
mediaPlayer.prepare();
}
catch (Exception e) { e.printStackTrace(); }
mediaPlayer.start();
mTimer.schedule(mMyTimerTask, 10, 5000);
}
else {
btnPlay.setImageResource(R.drawable.player_play);
mediaPlayer.pause();
mTimer.cancel();
}
// progressBar.setVisibility(ProgressBar.INVISIBLE);
}
Handler h = new Handler();
Runnable updateProgress = new Runnable() {
public void run() {
progressBar.setVisibility(View.VISIBLE);
}
};

ProgressDialog does not work even using thread

I am new in Android development. I am trying to show a ProgressDialog. I see lots of tutorial that say for showing dialog must use thread. As you can see snippet code is using thread.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
refreshFromFeed();
} catch (InterruptedException e) {
e.printStackTrace();
}
setContentView(R.layout.activity_main);
}
private void refreshFromFeed() throws InterruptedException {
ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
Thread th = new Thread(){
public void run(){
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000));
}
};
th.start();
dialog.dismiss();
}
protected void onRefresh(View view) throws InterruptedException {
refreshFromFeed();
}
The log shows it took 5 second, however, I cannot see any dialog on my screen and I can do anything on the screen. Even I use on a physical device. I've used debugging mode. There is no exception.
onRefresh is an event by onClick that declared on it's xml.
I've made a little bit changes of your code read it carefully.
private void refreshFromFeed() throws InterruptedException {
ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
Thread th = new Thread(){
public void run(){
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
try {
Thread.sleep(5000);
dialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo",String.valueOf(System.currentTimeMillis()/1000));
}
};
th.start();
}
private void refreshFromFeed() throws InterruptedException {
final ProgressDialog dialog = ProgressDialog.show(getActivity(),"Loading","Wake up after some sleep");
Thread th = new Thread() {
public void run() {
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis() / 1000));
try {
Thread.sleep(5000);
dialog.dismiss(); // dismiss your dialog here
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo", String.valueOf(System.currentTimeMillis() / 1000));
}
};
th.start();
}
You still run the ProgressDialog in your UI thread.
ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
New thread created after this line, not before this line!
You are dismissing your dialog just right after showing it.
Maybe you want to move your "dialog.dismiss();" to inside the thread. Remember that you need to dismiss the dialog on UI Thread, otherwise it will crash your app:
private void refreshFromFeed() throws InterruptedException {
final ProgressDialog dialog = ProgressDialog.show(this,"Loading","Wake up after some sleep");
Thread th = new Thread(){
public void run(){
Log.d("TimeFrom", String.valueOf(System.currentTimeMillis()/1000));
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("TimeTo", String.valueOf(System.currentTimeMillis()/1000));
runOnUiThread(new Runnable() {
#Override
public void run() {
dialog.dismiss();
}
});
}
};
th.start();
}
I see lots of tutorial that say for showing dialog must use thread.
You don't explicitly need a thread to show a ProgressDialog, this is just an example to dismiss it after 5000 ms

ProgressDialog doesn't show a progress

I use a simple example of ProgressDialog usage. Author of this code sure that his code is right and works good.
ProgressDialog barProgressDialog;
Handler updateBarHandler;
public void launchBarDialog() {
barProgressDialog = new ProgressDialog(getActivity());
barProgressDialog.setTitle("Downloading Image ...");
barProgressDialog.setMessage("Download in progress ...");
barProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
barProgressDialog.setProgress(0);
barProgressDialog.setMax(20);
barProgressDialog.show();
new Thread(new Runnable() {
#Override
public void run() {
try {
// Here you should write your time consuming task...
while (barProgressDialog.getProgress() <= barProgressDialog.getMax()) {
Thread.sleep(2000);
updateBarHandler.post(new Runnable() {
public void run() {
barProgressDialog.incrementProgressBy(2);
}
});
if (barProgressDialog.getProgress() == barProgressDialog.getMax()) {
barProgressDialog.dismiss();
}
}
} catch (Exception e) {
}
}
}).start();
}
But when i run this code in my project, I see that ProgressDialog is always show a 0 value as a progress. What do i wrong?
Try the following code:
private ProgressDialog barProgressDialog;
public void launchBarDialog() {
barProgressDialog = new ProgressDialog(this);
barProgressDialog.setTitle("Downloading Image ...");
barProgressDialog.setMessage("Download in progress ...");
barProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
barProgressDialog.setProgress(0);
barProgressDialog.setMax(20);
barProgressDialog.show();
new Thread(new Runnable() {
#Override
public void run() {
try {
// Here you should write your time consuming task...
while (barProgressDialog.getProgress() <= barProgressDialog.getMax()) {
Thread.sleep(2000);
barProgressDialog.incrementProgressBy(2);
if (barProgressDialog.getProgress() == barProgressDialog.getMax()) {
barProgressDialog.dismiss();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Log.d("", BuildConfig.VERSION_NAME);
}

Run a thread in android after one thread completes its execution

I want run run a thread after one thread completes its execution.
Here I am using progress bar, after progress bar completes the method do1() should execute but when I am run the application the application force close.
here is my code..
public void onenc(View view) {
progressBar = new ProgressDialog(view.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("Ecoding Text ...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
//reset progress bar status
progressBarStatus = 0;
//reset filesize
fileSize = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// ok, file is downloaded,
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();
Thread tt =new Thread(new Runnable() {
public void run() {
do1();
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
tt.start();
}
I have also tried AsyncTask but both run simultaneously.
Please help me I am a newbie in android.
Thanks in advance.
Update:
After AsyncTask
class MyFirstTask extends AsyncTask<String, Boolean, Boolean> {
#Override
protected Boolean doInBackground(String... params) {
return null;
//Do Stuff
}
public void progressUpdate(Integer progress) {
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// ok, file is downloaded,
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();
}
#Override
protected void onPostExecute(Boolean result) {
//Call your next task
Thread tt =new Thread(new Runnable() {
public void run() {
do1();
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
tt.start();
}
}
Now not even a single thread is executing the application force closes
After Another Update
class MyFirstTask extends AsyncTask<String, Boolean, Boolean> {
#Override
protected Boolean doInBackground(String... params) {
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// ok, file is downloaded,
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
// close the progress bar dialog
progressBar.dismiss();
}
}
}).start();
return null;
//Do Stuff
}
public void progressUpdate(Integer progress) {
}
#Override
protected void onPostExecute(Boolean result) {
do1();
//Call your next task
/* Thread tt =new Thread(new Runnable() {
public void run() {
do1();
try {
Thread.sleep(1100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
tt.start();*/
}
}
Now the function do1() executes before progress bar completes.
Finally Solved it....Here is the answer.
Hope it will help others
class MyFirstTask extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.show();
}
#Override
protected String doInBackground(String... params) {
int i=0;
while(fileSize<100)
{
fileSize=fileSize+1;
publishProgress(""+(int)(fileSize));
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
publishProgress(""+(int)(fileSize));
return null;
}
public void onProgressUpdate(String... progress) {
progressBar.setProgress(Integer.parseInt(progress[0]));
}
#Override
protected void onPostExecute(String result) {
progressBar.dismiss();
do1();
}
}
Don't use Thread() at all. Use AsyncTask as it makes life easier and implement the onPostExecute() method to call the next AsyncTask
class MyFirstTask extends AsyncTask<String, Boolean, Boolean> {
#Override
protected Boolean doInBackground(String... params) {
//Do Stuff that takes ages (background thread)
for(int i=0;i<100;i++){
doStuff();
Thread.sleep(1000L); //sleep because I'm just tired
publishProgress(i);
Thread.sleep(2000L); //sleep some more
}
}
#Override
public void progressUpdate(Integer progress) {
//Update progress bar (ui thread)
progressBar.setProgress(progress);
}
#Override
protected void onPostExecute(Boolean result) {
//Call your next task (ui thread)
new MyNextTask().execute();
}
Start your first task
new MyFirstTask().execute();
You can simply use join() method.
Thread first = new Thread();
Thread secThread =new Thread();
first.start();
first.join();
secThread.start();
but do not do this on main thread.

Categories

Resources