How to get a progress bar on button click? - android

I am developing an android application in which I have passed a string from one activity to another activity, after getting it from a edit text box by clicking on "OK" button.The string is a url which gives a rss feed.So it takes a little bit time to load it .I want to show a progress bar to keep the interface interactive.How can achieve a progress bar or a progress dialog on that same button click?
My code Snippet for activity is
EditText Urlis=(EditText)findViewById(R.id.entry);
final Button button = (Button) findViewById(R.id.ok);
final Intent i=new Intent(this , RSSReder.class);
final String choice=Urlis.getText().toString();
i.putExtra("key", choice);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(i);
}
});
}
}
some part of the code of next activity is
public class RSSReder extends Activity implements OnItemClickListener
{
public String RSSFEEDOFCHOICE;
public final String tag = "RSSReader";
private RSSFed feed = null;
/** Called when the activity is first created. */
public void onCreate(Bundle abc) {
super.onCreate(abc);
setContentView(R.layout.next1);
Intent i = getIntent();
RSSFEEDOFCHOICE =i.getStringExtra("key");
// go get our feed!
feed = getFeed(RSSFEEDOFCHOICE);
// display UI
UpdateDisplay();
}

You can show the ProgressDialog in the NewActivity if it takes some time in getting rss feeds:
take help from the following code:
dialog = ProgressDialog.show(mParent,"","Loading,Please wait...", true);
final Thread t=new Thread(new Runnable() {
public void run() {
//get your rss feeds here
}
});
t.start();
Thread t1=new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
try {
t.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Handler().post(new Runnable() {
public void run() {
dialog.cancel();
UpdateDisplay();
}
});
}
});
t1.start();
Hope you can understand what the above code is doing. Customized it a bit and use...:)

Related

How to wait for an activity to fully load?

I'm making an activity where I'm planning to display a progress bar and move to a second activity after five seconds. I used Thread.sleep to do this.
Here is the code for the activity with the spinner.
public class WaitScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.waitscreen);
}
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
try{
for(int i=0;i<5;i++){
Thread.sleep(1000);
}
Toast.makeText(getApplicationContext(), "Spinner complete!", Toast.LENGTH_LONG).show();
}
catch(InterruptedException e){
}
Intent i = new Intent("emergency.app.NEWACTIVITY");
startActivity(i);
}
}
My main activity only has a button that leads to this page on being clicked. Problem is, it displays the main activity for five seconds, then this activity very briefly and then the third activity. How do I make the thread start after WaitScreen is fully loaded? As you can see, I tried using the onStart method to do the trick. Or if you could tell me another way to introduce a five second delay, that would be helpful too!
Use a Handler to post a Runnable, like so:
public class WaitScreen extends Activity {
private Runnable task = new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "Spinner complete!", Toast.LENGTH_LONG).show();
Intent i = new Intent("emergency.app.NEWACTIVITY");
startActivity(i);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.waitscreen);
Handler handler = new Handler();
handler.postDelayed(task, 5000);
}
}
The Runnable task will execute after 5 seconds.
http://developer.android.com/reference/android/os/Handler.html

Android progressDialog dont't show if startActivity(myIntent)

I am using this simple code
public class Main extends Activity {
private ProgressDialog progressDialog;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//bouton Exemples de prix
findViewById(R.id.button1).setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
//start the progress dialog
runDialog(4);
Intent myIntent = new Intent(getBaseContext(), Exemple_prix.class);
startActivity(myIntent);
}
}
);
}
private void runDialog(final int seconds)
{
progressDialog = ProgressDialog.show(this, "", "Chargement...");
new Thread(new Runnable(){
public void run(){
try {
Thread.sleep(seconds * 1000);
progressDialog.dismiss();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
The progressDialog doesn't show but it works if I disable
//startActivity(myIntent);
Your progress dialog is tied to the activity, so it shows on top of the first activity, then immediately you're starting the new activity which covers both the old activity and the progress dialog. Is there a particular reason why you're showing the progress dialog? If it's related to work in the second activity, you should show it there instead.
On a side note, don't create a new thread just to sleep like that. Just use postDelayed.

show progressbar on button click when going from 1 intent to other and data is coming from server

I have a button in one page and when I click on that button I
am able to go another activity through Intent(), but onbuttonclick()
in which activity I am going in that activity data in spinner
coming from server means on button click
I load that data on spinner from server.so it takes times for moving my button click activity
to other activity so I want to show progress bar when my button is clicked
and untill data is not coming from server...how to achieve this..and I want to show progress bar
on buttonclick page means on my first activity when I click the button.
My code of of on button click is given below.
cuurentloc.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
Intent i = new Intent(MainMenu.this, currentlocmap.class);
startActivity(i);
}
});
Actually I know asynchronous task but using this I will be able to show progress bar on 2nd activity, I want to show it on my first activity until data is not loaded in second activity, so I want progree bar above the button on first activity, and when data is loaded on second activity it moves to second.
You need to use AsyncTask as the way I am guiding here.
Create Async Task in first activity. On button click event call that AsyncTask. In background do loading data from server. and onPostExecute start second activity
cuurentloc.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v) {
new ProgressTask(MyClassName.class).execute(null);
}
});
Async Task
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog;
List<Message> titles;
private ListActivity activity;
//private List<Message> messages;
public ProgressTask(ListActivity activity) {
this.activity = activity;
context = activity;
dialog = new ProgressDialog(context);
}
/** progress dialog to show user that the backup is processing. */
/** application context. */
private Context context;
protected void onPreExecute() {
this.dialog.setMessage("Progress start");
this.dialog.show();
}
#Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
Intent i = new Intent(MainMenu.this, currentlocmap.class);
startActivity(i);
}
protected Boolean doInBackground(final String... args) {
try{
//load data from server
} catch (Exception e){
Log.e("tag", "error", e);
return false;
}
}
}
}
Thanks
Deepak
have a look at this code
package com.exercise.AndroidBackgroundThread;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidBackgroundThread extends Activity {
Thread backgroundThread;
TextView myText;
boolean myTextOn = true;
boolean running = false;
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
//super.handleMessage(msg);
if (myTextOn){
myTextOn = false;
myText.setVisibility(View.GONE);
}
else{
myTextOn = true;
myText.setVisibility(View.VISIBLE);
}
}
};
void setRunning(boolean b){
running = b;
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myText = (TextView)findViewById(R.id.mytext);
Toast.makeText(this, "onCreate()", Toast.LENGTH_LONG).show();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(this, "onStart()", Toast.LENGTH_LONG).show();
backgroundThread = new Thread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
while(running){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.sendMessage(handler.obtainMessage());
}
}
});
setRunning(true);
backgroundThread.start();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
boolean retry = true;
setRunning(false);
while(retry){
try {
backgroundThread.join();
retry = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(this, "onStop()", Toast.LENGTH_LONG).show();
}
}
and for more detail look at this guide http://tech-aamir.blogspot.in/2012/06/how-to-make-progress-bar-when.html
Best of luck
aamirkhan i.
To build on the first answer, since you are familiar with AsyncTask. You can have the AsyncTask perform the work to retrieve whatever data you'll need in your first activity. And during that process, you display your progress bar. Once the AsyncTask completes, you remove the progress bar, put your data in a bundle (by calling putExtras), and send it off with your intent to start the 2nd Activity.
You can use the ProgressBar or ProgressDialog in the currentlocmap class.
Use AsyncTask class for that and when the data is fetched, set the layout using setContentView() and dismiss the ProgressDialog.
Refer to these links:
Fetch data from server and refresh UI when data is fetched?
How to start and finish progressBar dynamically in android

android progressBar problem

i have a button that on click is loading rss feed.i want to load a progress bar until my list opens.i have created a progressbar,it works,but as i press the return button to return to the main menu the progress bar appears again and it doesnt stop(and not even let me see my menu).this is my code
ProgressBar myProgressBar;
int myProgress = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main1);
Button nea = (Button) findViewById(R.id.nea);
nea.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
setContentView(R.layout.bar);
myProgressBar=(ProgressBar)findViewById(R.id.bar);
new Thread(myThread).start();
Intent myIntent = new Intent(view.getContext(), nea.class);
startActivityForResult(myIntent, 0);
}
});
}
and then,out of the onCreate
private Runnable myThread = new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
while (myProgress<100){
try{
myHandle.sendMessage(myHandle.obtainMessage());
Thread.sleep(1000);
}
catch(Throwable t){
}
}
}
Handler myHandle = new Handler(){
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
myProgress++;
myProgressBar.setProgress(myProgress);
}
};
};
Your Activity isn't getting recreated when you return from the startActivityForResult Activity you started, so you contentView is stuck as R.layout.bar .
Try overriding the onActivityResult(int requestCode, int resultCode, Intent data) method and
calling setContentView(R.layout.main1); inside of it to reset your view. You may need to add the listener to the button again.

Android: Progress Dialog Not Displaying

I have a main activity that launches a child activity using the following code:
Intent intent = new Intent();
intent.setClassName(MyChildActivity.class.getPackage().getName(), MyChildActivity.class.getName());
((Activity)context).startActivity(intent);
I am trying to perform a time-consuming task in the child activity and would like to display a ProgressDialog while I do so. My code looks like this:
private ProgressDialog _progressDialog;
private OnClickListener btn_onClick = new OnClickListener() {
public void onClick(View v) {
_progressDialog = ProgressDialog.show(
v.getContext(),
"Please wait",
"Performing task..."
);
TaskThread t = new ExportThread(v.getContext());
t.start();
}
};
private class TaskThread extends Thread{
private Context _context;
public TaskThread(Context context) {
_context = context;
}
private Handler _handler = new Handler() {
#Override
public void handleMessage(Message msg) {
_progressDialog.dismiss();
}
};
#Override
public void run() {
performTask(_context);
_handler.sendEmptyMessage(0);
}
}
For some reason, the ProgressDialog is not displaying. If I use that same code in the main activity, it works - but not in the child activity. In addition, the following code also fails to display the ProgressDialog (but the Toast does display):
private ProgressDialog _progressDialog;
private OnClickListener _btn_onClick = new OnClickListener() {
public void onClick(View v) {
_progressDialog = ProgressDialog.show(
v.getContext(),
"Please wait",
"Performing task..."
);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
_progressDialog.dismiss();
Toast.makeText(v.getContext(), "Done with progress dialog.", Toast.LENGTH_SHORT).show();
}
};
Any ideas out there? Are we not allowed to display a ProgressDialog from a child activity?
Thank you.
why use thread instead of async task?
Async task implements the method onProgressUpdate and publishProgress which makes it easy to display and update UI/progress dialogs.
Here is some example code: http://android-projects.de/2010/12/08/threading-in-android-apps-wir-entwickeln-einen-zahler/

Categories

Resources