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.
Related
I want to show a ProgressDialog while jumping from my LoginActivity to my HomeActivity, basically while HomeActivity is loading.
I am using a separate thread to call the intent of the HomeActivity while I am showing the ProgressDialog from the main thread in LoginActivity.
Everything seems to be working fine, it is just that the ProgressDialog is displayed but there is no animated spinner.
I am wondering if i am doing this correctly.
Here the relevant code from LoginActivity:
public class LoginActivity extends AppCompatActivity {
private EditText userID, userPass;
private ProgressDialog dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userID = (EditText) findViewById(R.id.edtX_srcLogin_userID);
userPass = (EditText) findViewById(R.id.edtX_srcLogin_userpass);
final Button bt_submit = (Button) findViewById(R.id.bT_scrLogin_submit);
bt_submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
parseUserCredentials(userID.getText().toString(), userID.getText().toString());
}
});
}
private void parseUserCredentials(String userId, String userPassword) {
if ( userId.equals("userid") && userPassword.equals("1234") ) {
dialog = ProgressDialog.show(LoginActivity.this, "Checking credentials",
"Please wait...", true, false);
newThreadLoadActivity();
}else {
userID.setText("");
userPass.setText("");
userID.requestFocus();
dialog.cancel();
}
}
private void newThreadLoadActivity(){
new Thread() {
public void run() {
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}.start();
}
#Override
protected void onStop() {
super.onStop();
dialog.cancel();
}
}
EDIT
The main idea is to show the ProgressDialog whileHomeActivity loads, and cancel the ProgressDialog when HomeActivity is ready to be displayed.
Use the below code.It will make the delay for 10sec while moving to the next activity so your progress bar make be visible.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
dialog.cancel();
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}, 10000);
I'm having some problems displaying a ProgressDialog. I have a method that scrapes information from a website, and I want to show the user some kind of "Loading" window instead of the app just looking like it is hanging for a second or two when it is working.
Everything works fine when I don't implement a ProgressDialog & Thread, but as soon as I try to implement a Thread to do the heavy lifting, the AboutMe View window is empty.
I have a MainActivity with a TextView that registers a OnClickListener.
A Click on the TextView does a:
startActivity(new Intent(getBaseContext(), AboutMe.class));
This is most of the AboutMe.class Activity:
public class AboutMe extends Activity {
private ProgressDialog aboutMeProgressDialog;
private String htmlAboutMe = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
getAboutMe(); // Get information from Internet
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFeatureDrawableResource(Window.FEATURE_NO_TITLE, android.R.drawable.ic_dialog_alert);
setContentView(R.layout.abutme);
TextView tvAbout = (TextView) findViewById(R.id.aboutMe);
tvAbout.setText(Html.fromHtml(htmlAboutMe));
}
private void getAboutMe() {
try {
aboutMeProgressDialog = ProgressDialog.show(AboutMe.this, "", "Loading");
new Thread() {
#Override
public void run() {
try {
/** Code to scape webpage **/
}
catch (Exception exp) {
exp.printStackTrace();
}
handler.sendEmptyMessage(0);
}
}.start();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private final Handler handler = new Handler() {
#Override
public void handleMessage(final Message msg) {
aboutMeProgressDialog.dismiss();
}
};
I'm clearly missing out on something trivial, but I've tried to Google just about everything I can think of, but still can't get a Thread together with ProgressDialog to work for me.
please use run on ui thread method
instead of handler.sendEmptyMessage(0); use this code and remove handle message
runOnUiThread(new Runnable() {
#Override
public void run() {
aboutMeProgressDialog.dismiss();
}
});
dude let me know if this was successful,it works most of times
please call getAboutMe() method after calling super.onCreate(savedInstanceState);
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...:)
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/
This may be a simple question but i am a beginner ,i need your suggestion on this.
i have two Activities A1 and A2 .When i click the image on A1 screen i have to display progress bar until A2 screen appears(A2 activity has huge task to do).I tried
image.setOnClickListener(new ImageView.OnClickListener() {
public void onClick(View v)
{
myProgressDialog = ProgressDialog.show(A1.this,
"Please wait...", "Loading...", true);
new Thread() {
public void run() {
try{
Intent i = new Intent(A1.this,.A2.class);
startActivity(i);
} catch (Exception e) { }
// Dismiss the Dialog
myProgressDialog.dismiss();
}
}.start();
}
});
This couldn't display progress bar .I know that i am making a mistake but i couldn't figure out
Your best bet is to show the progress dialog in the A2 activity. Once you start an Activity, the previous activity goes into the background, so the progress dialog wouldn't display anyway.
First of all , the progress dialog needs to be called on a separate thread . Use the AsyncTask<> to display the dialog and at the same time perform some operation in the background . A sample code might be something like this
class hello extends AsyncTask<Void,Void,Void>
{
ProgressDialog dialog=null;
Intent i;
#Override
protected void onPreExecute()
{
dialog=ProgressDialog.show(A1.this,"PLEASE WAIT","LOADING CONTENTS ..",true);
}
#Override
protected void onPostExecute(Void result)
{
if(dialog.isShowing())
{
dialog.dismiss();
startActivity(i);
}
}
#Override
protected Void doInBackground(Void... params)
{
i = new Intent(A1.this,.A2.class);
return null;
}