when click the grid item, i want to show a progressbar between the time of next Activity shown. then the second activity has a custom listview. there also i want to show a progressbar. how to do that?
I'm just learning myself and I haven't had a chance to inspect the source, but I know that the RedditIsFun app (for Reddit, of course) does this in-between loading the next links, and loading comments. Check out the source.
You should try this,
First Create the ProgressDilog in Activity. . .
private ProgressDialog progressDialog;
Now, Set the ProgressDialog in the Any action where u want to Take Some time
progressDialog = ProgressDialog.show(FoodDriveModule.this, "", "Loading...");
Now use Thread to Handle the Progressbar
new Thread()
{
public void run()
{
try
{
sleep(1500);
// Now Do some Task whatever u want to do
}
catch(Exception e)
{
Log.e("tag",e.getMessage());
}
}.start();
Thats it. Hope this will help you.
Related
As and when i click to button, it waits for 20 seconds and then getting text to text view, my requirement is that after 1 second, progress bar has to increment by 5 values.
Can any one guide me in following code
public class ProgressBar1 extends Activity{
TextView tvpbview;
ProgressDialog pd1;
ProgressBar pbhr1;
Button btnhp1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressbar1);
btnhp1 = (Button) findViewById(R.id.btnProgress);
pbhr1 = (ProgressBar) findViewById(R.id.pbHori1);
pbhr1.setMax(100);
btnhp1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//--- For Horizontal Progress Bar------
for(int i=0;i<=20;i++)
{
pbhr1.incrementProgressBy(5);
tvpbview.setText(""+pbhr1.getProgress()+"% done");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
You approach is incorrect, you need to use correct form of Threads. Especially for your case i recommend to you use following:
Handler
AsyncTask
Most likely you need to read some tutorial so have look at
Android Threads, Handlers and AsyncTask -
Tutorial
this is a great source.
Note: AsyncTask is more complex than Handler and also is generic-type and is especially designed for updating UI with some progress.
create an asynchronous task. Android provides the AsyncTask class for this purpose. You create a progress bar on the UI thread, then call the publishProgress() method to update your progress bar. It's really simple to use, the documentation is excellent.
try this
After they have been initially set, if you want to then change the values you need to set it to zero in between:
ProgressBar dataProgressBar = (ProgressBar) findViewById(R.id.progressBarData);
//set to 0 first to stop the bug from happening
dataProgressBar.setMax(0);
dataProgressBar.setProgress(0);
//set your new values
dataProgressBar.setMax((int) dataAllowanceValue);
dataProgressBar.setProgress((int) dataMBytes);
I have a fragment with some buttons in it, when a button is clicked it should show a ProgressDialog, load an array of bitmaps and show it in the fragment in a gallery, dismiss ProgressDialog.
But the ProgressDialog doesn't show immediately, it take something like 1 or 2 seconds and it just blink at the moment when my gallery is show.
Im doing this after click:
try{
progress = ProgressDialog.show(activity, "", "Loading images", true);
//load images
//show gallery
}catch(){
//...
}finally{
handler.sendEmptyMessage(0);
}
My Handler at onCreate:
handler = new Handler() {
public void handleMessage(Message msg) {
progress.dismiss();
}
};
Im using Android 3.1
Logcat shows anything :(
03-09 13:17:32.310: D/DEBUG(5695): before show()
03-09 13:17:32.350: D/DEBUG(5695): after show()
You are loading the images on the main UI thread - you should do this in a background process as it may cause your UI to become unresponsive (and cause your ProgressDialog to show up at the wrong time).
You should look into using an AsyncTask to carry out loading of the images in the background.
Display the ProgressDialog in AsyncTask.onPreExecute, load images in AsyncTask.doInBackground and dismiss the dialog in AsyncTask.onPostExecute.
Documentation does not tell much about setIndeterminate(boolean), so I'm not sure. But I use this in my app, and it works:
ProgressDialog fDialog = new ProgressDialog(your-context);
fDialog.setMessage(your-message);
fDialog.setIndeterminate(true);
// fDialog.setCancelable(cancelable);
fDialog.show();
Could you try it?
I'm using a standard ProgressDialog implementation: I set up a thread to run a long task, and when it's done it dismisses the ProgressDialog.
#Override
public void onCreate( Bundle bundle ) {
super.onCreate( bundle );
context = this;
progress = ProgressDialog.show( this, "Running", "Please wait..", true, false);
progress.setOnDismissListener( new OnDismissListener() {
public void onDismiss(DialogInterface dialog) {
showResults();
}
});
new DeleteThread().start();
}
And the Thread looks like this:
private class DeleteThread extends Thread {
public DeleteThread() {}
#Override
public void run() {
// long process during which we populate
// a LinearLayout with many other Views
progress.dismiss();
}
}
And in showResults() we take the LinearLayout now filled with Views and set it as the content of an AlertDialog.
The problem is, the ProgressDialog goes away and there's still a long period of time (10-12sec) where nothing is happening before the AlertDialog pops up. Is there a way to make this transition instantaneously?
I know this answer might be coming in a bit late, but what the heck.
I see that you're triggering the showResults() method from the onDismiss callback. I would suggest you do it the other way around. To do that you may need to use the AsyncTask class.
The AsyncTask allows you to easily setup tasks to run in a spawned thread, to update a progress bar if needed (not required, but might be a nice touch in your case) and of course to run some code on the UI thread once the spawned thread completes. For your case, I would place your populating of layoutview in the AsyncTask.doInBackground() method. Then, you'd want to add the showResults() method call in AsyncTask.onPostExecute(). At the bottom of onPostExecute() you should then dismiss your progress dialog. That SHOULD give you the results you want.
As a bonus, you might want to create a constructor for your AsyncTask class and place the creation of the ProgressDialog in there. That way the ProgressDialog will be completely encapsulated within that class and everything is nice and clean. So, something like this:
class MyAsyncClass extends AsyncClass
{
ProgressDialog m_progress;
public MyAsyncClass()
{
m_progress = ProgressDialog.show( this, "Running", "Please wait..", true, false);
}
protected Long doInBackground(Object... data) {
// Do your long process of populating a LinearLayout with many other Views
}
protected void onPostExecute(Long result) {
showResults();
m_progress.dismiss();
}
}
NOTE: Above needs some extra parameters to the class, but you get the idea.
I'm going to venture a guess as a novice Android developer and suggest that while the pointers are generated for the LinearLayout, the layout itself hasn't been populated with content and therefor is populating when you call progress.dismiss()
You will probably have to render the view prior to setting to the AlertDialogs content. http://developer.android.com/guide/topics/ui/how-android-draws.html might have some help in that respect, unfortunately my laptop is have HDD issues so I don't have the Android Development Environment installed and can't try it. But I'd try an invalidate on the LinearLayout right before progress.dismiss(), I hope that helps :S
Make use of something like this below and use an AsyncTask that dismisses the dialog once it is done:
final ProgressDialog progressDialog = new ProgressDialog(v.getContext());
progressDialog.setCancelable(false);
progressDialog.setMessage(v.getContext().getText(R.string.defaultQuantities));
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
new CountDownTimer(250, 250) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
if (progressDialog.getProgress() < 100) {
progressDialog.show();
}
}
}.start();
Can I use progress bar in android, without the thread?
HERE IS CODE OF MY CURRENT WAY OF IMPLEMENTING PROGRESS DIALOG
// Adding Progress bar
String[][] data; //Global variable
//called on onCreate() or onItemSelected
final ProgressDialog myProgressDialog;
myProgressDialog = ProgressDialog.show(ListingPage.this,"Please Wait", "Loading Date", true);
new Thread() {
public void run() {
try{
setSelected();
sleep(5000);
} catch (Exception e) { }
myProgressDialog.dismiss();
}
}.start();
populateList(Split.splitToTwoDimArray(data)); // populates the list view
HOPE ABOVE HELPS, IF USING THREAD THE LIST IS NOT BEING POPULATED.
Sure, you can always set the progress manually via
progressBar.setProgress(int progress);
Above question/added code is a bit confusing cause you asked how to use the progress bar without a thread but now in your code you're using a thread. I thought that you initially wanted to avoid.
Anyway, maybe you should use an AsyncTask instead of the Thread, which allows you to modify anything in the main UI thread.
https://sites.google.com/site/androidhowto/how-to-1/create-a-custom-progress-bar-using-asynctask
http://developer.android.com/reference/android/os/AsyncTask.html
I'm trying to create a ProgressDialog for an Android-App (just a simple one showing the user that stuff is happening, no buttons or anything) but I can't get it right. I've been through forums and tutorials as well as the Sample-Code that comes with the SDK, but to no avail.
This is what I got:
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
(...)
ProgressDialog pd = new ProgressDialog(MyApp.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Working...");
pd.setIndeterminate(true);
pd.setCancelable(false);
// now fetch the results
(...long time calculations here...)
// remove progress dialog
pd.dismiss();
I've also tried adding pd.show(); and messed around with the parameter in new ProgressDialog resulting in nothing at all (except errors that the chosen parameter won't work), meaning: the ProgressDialog won't ever show up. The app just keeps running as if I never added the dialog.
I don't know if I'm creating the dialog at the right place, I moved it around a bit but that, too, didnt't help. Maybe I'm in the wrong context? The above code is inside private ViewGroup _createInputForm() in MyApp.
Any hint is appreciated,
you have to call pd.show before the long calculation starts and then the calculation has to run in a separate thread. A soon as this thread is finished, you have to call pd.dismiss() to close the prgoress dialog.
here you can see an example:
the progressdialog is created and displayed and a thread is called to run a heavy calculation:
#Override
public void onClick(View v) {
pd = ProgressDialog.show(lexs, "Search", "Searching...", true, false);
Search search = new Search( ... );
SearchThread searchThread = new SearchThread(search);
searchThread.start();
}
and here the thread:
private class SearchThread extends Thread {
private Search search;
public SearchThread(Search search) {
this.search = search;
}
#Override
public void run() {
search.search();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
displaySearchResults(search);
pd.dismiss();
}
};
}
I am giving you a solution for it,
try this...
First define the Progress Dialog in the Activity before onCreate() method
private ProgressDialog progressDialog;
Now in the onCreate method you might have the Any button click on which you will change the Activity on any action. Just set the Progress Bar there.
progressDialog = ProgressDialog.show(FoodDriveModule.this, "", "Loading...");
Now use thread to handle the Progress Bar to Display and hide
new Thread()
{
public void run()
{
try
{
sleep(1500);
// do the background process or any work that takes time to see progress dialog
}
catch (Exception e)
{
Log.e("tag",e.getMessage());
}
// dismiss the progress dialog
progressDialog.dismiss();
}
}.start();
That is all!
Progress Dialog doesn't show because you have to use a separated thread. The best practices in Android is to use AsyncTask ( highly recommended ).
See also this answer.
This is also possible by using AsyncTask. This class creates a thread for you. You should subclass it and fill in the doInBackground(...) method.