I'm a "fresh" Android developer and i tried the horizontal ProgressBar.
I have a little issue with it, progress stops in the middle and does not continue till the end of the bar.
Can you help me please?
this is my code
progress=0;
mProgress = (ProgressBar) findViewById(R.id.progressBar1);
mProgress.setMax(200);
// Start lengthy operation in a background thread
new Thread(new Runnable()
{
public void run()
{
while (mProgressStatus < 100)
{
mProgressStatus = doWork();
// Update the progress bar
mHandler.post(new Runnable()
{
public void run()
{
mProgress.incrementProgressBy(20);
mProgress.setProgress(mProgressStatus);
}
});
}
// Cache la progressBar
mHandler.post(new Runnable()
{
#Override
public void run()
{
mProgress.setVisibility(4);
}
});
}
//---------------------------------Work to do-----------------------
private int doWork()
{
try{
Thread.sleep(50);//5 secondes
}catch(InterruptedException e)
{
e.printStackTrace();
}
return ++progress;
}
//------------------------------------------------------------------
}).start();
}
You set the progress bar maximum value to 200:
mProgress.setMax(200);
and later in code you stop when it raises to 100 (half filled). Either set the maximum to 100 or keep running till 200.
Related
I am trying to copy the design of these progress bars :
I have searched here and only found how to populate the progress bar using:
mProgress = (ProgressBar) findViewById(R.id.progress_bar);
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) {
mProgressStatus = doWork();
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
}
}).start();
Please anyone have an idea how to change the progress bar background to as shown
in the image above
I am new to android, don't have much experience to manage threads. I am working on an activity where I want to display progress bar for say 5 seconds and then repeat. During those 5 seconds, I will display some text for the user to work on the text. I want to repeat this for say N times.
Currently, I have the following code that works for 1 such progress. I tried looping it but it didn't help, as threads executed at same time. How can I repeat this for N number of times? Am I on the correct path in order to solve my the problem?
public class test extends Activity {
private ProgressBar progressBar;
private int progressStatus = 0;
private TextView textView;
private Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_loop);
progressBar = (ProgressBar) findViewById(R.id.progressBar1);
textView = (TextView) findViewById(R.id.textView1);
progressBar.setScaleY(3f);
// Start long running operation in a background thread
for(int i=0; i<5; i++)
Progress();
}
public void Progress(){
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}
I am not sure I would recommend using the pattern you are following but here is the way to get the threads to run one after another rather than all at the same time:
public void progress(final int numberOfRuns) {
if (numberOfRuns <= 0 ) {
return;
}
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 1;
// Update the progress bar and display the
//current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText(progressStatus + "/" + progressBar.getMax());
// For the UI Changes. Eg update the loop number
myTextView.setText(Integer.toString(totalLoop));
}
});
try {
// Sleep for 200 milliseconds.
//Just to display the progress slowly
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
progressStatus = 0;
totalLoop = totalLoop+1;
progress(numberOfRuns - 1);
}
}).start();
}
then just call progress(numberOfRuns), no need for any loops.
I am developing a quiz application in which I am using runnable thread for seekbar functionality. seekbar shows how much time is remaining. Now I want to stop the seekbar when a answer button is click.
here is my code for runnable thread.
new Thread(new Runnable() {
public void run() {
while (seekbarStatus < 100) {
seekbarStatus = LoadingStatus();
// sleep 1 second to show the progress
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
seekbar.setProgress(seekbarStatus);
}
});
}
if (seekbarStatus >= 100) {
// sleep for 2 seconds, so that you can see the 100% of file download
runOnUiThread(new Runnable() {
#Override
public void run() {
if(!(Ans_1.getTag()=="clicked" ||Ans_2.getTag()=="clicked" || Ans_3.getTag()=="clicked" ))
{
Ans_1.setEnabled(false);
Ans_2.setEnabled(false);
Ans_3.setEnabled(false);
Ans_1.setBackgroundColor(Color.GREEN);
points.setText("0 Punkt");
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
showDialogView();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
public void showDialogView() {
// TODO Auto-generated method stub
dialog.show();
}
});
}
}
}).start();
please help me. Any help would be appreciated.
I am not getting how to solve it.
Thanks in advance.
Just add an instance variable:
private boolean isCancelled = false;
Then in the button onClick:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
isCancelled = true;
}
});
Then change
while (seekbarStatus < 100) {
to
while (seekbarStatus < 100 && !isCancelled) {
All:
I really don't grok handlers yet. I thought that the code below -- modified so that, instead of using a handler, the UI widget (progress bar) was accessed directly -- would cause a cross-threading exception. But it doesn't. So, my question is, shouldn't this code crash? And if it doesn't, then when do I need to use a handler?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
progress = 0;
progressBar = (ProgressBar) findViewById(R.id.progressbar);
progressBar.setMax(200);
//---do some work in background thread---
new Thread(new Runnable()
{
public void run()
{
//ó-do some work hereó-
while (progressStatus < 200)
{
progressStatus = doSomeWork();
progressBar.setProgress(progressStatus); // not on UI thread
//ó-Update the progress baró- // so shouldn't it crash?
// handler.post(new Runnable()
// {
// public void run() {
// progressBar.setProgress(progressStatus);
// }
// });
}
//---hides the progress bar---
handler.post(new Runnable()
{
public void run()
{
//---0 - VISIBLE; 4 - INVISIBLE; 8 - GONE---
progressBar.setVisibility(View.GONE);
}
});
}
Nowadays, ProgressBar has logic that allows setProgress() to be called on a background thread. It checks to see what thread you are on and does its own post() of a Runnable if needed. You can see this in the source code.
Here is the problem
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
progressDialog = ProgressDialog.show(ContactMainActivity.this, "",
"Loading. Please wait...",true);
setContentView(R.layout.contactlist);
contactListView=getListView();
contactListView.setTextFilterEnabled(true);
contactListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Thread t = new Thread() {
public void run() {
try{
runOnUiThread(new Runnable() {
public void run() {
//stuff that updates ui
queryAllRawContacts();
progressDialog.dismiss();
}
});
registerForContextMenu(contactListView);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
}
};
t.start();
}
this is onCreate method of my activity class.
This activity actually one tab of a tabwidget. So when I click this tab and run the activity, it waits about 2 seconds and then show progress dialog like 10 millisec and updates the UI with the data. and dismiss progress dialog. What I would like to achieve is that as soon as the tab is clicked, the progress dialog will be shown and it takes about 2-3 seconds to load data. after loading and updating the UI the progress bar will be dismissed.
Thanks
queryAllRawContacts() needs to not be run on the UI thread. Change your code to the following:
Thread t = new Thread() {
public void run() {
queryAllRawContacts();
try{
runOnUiThread(new Runnable() {
public void run() {
//stuff that updates ui
progressDialog.dismiss();
}
});
registerForContextMenu(contactListView);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
}
};
t.start();