I am getting image from server and showing it on custom dialog and I want to show progress dialog while image is fetching.
I am using this
loadingDialog = ProgressDialog.show(getActivity(), "", "Loading. Please wait...", true);
but it Hides under the custom dialog.
Put this code in call of fetching an image from server
try {
progDialog = new ProgressDialog(Activity_FindInfo.this);
progDialog.setIndeterminate(true);
progDialog.setMessage("loading");
progDialog.setCancelable(false);
progDialog.show();
} catch (Exception ex) {
}
declare this in your class
private ProgressDialog progDialog;
after that add following code just before call to your fetching image..
try {
progDialog = new ProgressDialog(yourCustomDialog.getContext());
progDialog.setIndeterminate(true);
progDialog.setMessage("fetching image...");
progDialog.setCancelable(false);
progDialog.show();
} catch (Exception ex) {
}
then when image is fetched before that add this to dismiss dialog..
try {
if (progDialog != null && progDialog.isShowing()) {
progDialog.dismiss();
}
} catch (Exception ex) {
}
Related
I am creating an app in which I have to fetch some data from Internet and show it to user on UI.
I have create an progress bar in AsyncTask onPreExecute() method and in onPostExecute() method I have passed all data to another Activity and startActivty. Then I dismiss the progress dialog. But, it not fine.
As when the second activity loaded data on UI in onCreate method then ProgressBar hangs.
How do I solve this problem.
Here is my code:
#Override
protected void onPreExecute() {
proDialog = new ProgressDialog(mContext);
proDialog.setTitle("Support Manager");
proDialog.setMessage("Logging in ...");
proDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
proDialog.setIcon(R.drawable.ic_launcher);
proDialog.show();
super.onPreExecute();
}
#Override
protected RepresentativeData doInBackground(String... params) {
JSONObject result = null;
try {
result = client.reprentativeDetailNode(params[0]);
LoginJsonParsing loginJsonParsing = new LoginJsonParsing();
Details = loginJsonParsing
.Details(result);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return Details;
}
#Override
protected void onPostExecute(Data result) {
Intent secActivity = new Intent(mContext,YourRepresentative.class);
secAcitivity.putExtra("detail", result);
mContext.startActivity(secActivity);
proDialog.dismiss();
super.onPostExecute(result);
}
You can't have a progress to be displayed when switching to a new activity
I want to use a progress dialog in an activity named myActivity.
I launch it from a method in the activity:
progressDialog = ProgressDialog.show(myActivity.this, "", "loading ...");
but nothing appears. Why?
I've also tried this line:
progressDialog = ProgressDialog.show(myActivity.this, "", "loading ...",true);
with the same result.
Just this
//Declare progressDialog before so you can use .hide() later!
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
Please add runnable thread into your code
Example: https://abhiandroid.com/ui/progressdialog
or see this example code
progressDialog.show(); // Display Progress Dialog
progressDialog.setCancelable(false);
new Thread(new Runnable() {
#Override
public void run() {
try {
while (progressDialog.getProgress() <= progressDialog.getMax()) {
Thread.sleep(200);
handle.sendMessage(handle.obtainMessage());
if (progressDialog.getProgress() == progressDialog.getMax()) {
progressDialog.dismiss();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}}).start();
Im Adding Progress Dialog in some Activity .But im getting Exception mention in title.how to resolve it.
dialog = ProgressDialog.show(Notification.this, "loading please wait",
"Loading. Please wait...", true);
new Thread() {
public void run() {
try{
performBackgroundProcess1();
//sleep(3000,000);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
dialog.dismiss();
}
}.start();
Any thing wrong with this.all Background process is performed in performbackgroundprocess method.
You cant call dialog.dismiss(); in the background thread.
You can make Threads send messages to handlers when they are done and in the handler you can dismiss the dialog. Handlers work in ui thread
There is a tutorial about it
use runOnUiThread as:
new Thread() {
public void run() {
try{
performBackgroundProcess1();
//sleep(3000,000);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
CurrentActivity.this.runOnUiThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
}.start();
Here is my code segment I am trying to dismiss dialog but it is not getting dismissed and also i don't get any error on logcat.Please correct me where i am wrong ?
All the Log.v statement get executed.Even log.v statement after pd.dismiss() (Log.v("TAG","progress dismiss");) gets printed.
Please point my mistake or suggest some alternative way to dismiss the progressdialog.
btnSave.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//do something
} else {
Log.v("TAG","above progressDialog");
final ProgressDialog pd = new ProgressDialog(ChangePassword.this);
ProgressDialog.show(ChangePassword.this, "", "Loading...", false, true);
new Thread() {
public void run() {
try {
sleep(2000);
Log.v("TAG","in try block");
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
pd.dismiss();
// Log.v("TAG","progress dismiss");
}
}.start();
Log.v("TAG","after start");
public void run()
{
try{
if (!(txtOldPass.getText().toString())
.equals(SetGetValues.getPassword())) {
Toast.makeText(ChangePassword.this,
"Invalid Old Password", Toast.LENGTH_SHORT)
.show();
txtOldPass.setText("");
txtNewPass.setText("");
txtCnfPass.setText("");
} else {
if (!(txtNewPass.getText().toString())
.equals(txtCnfPass.getText().toString())) {
Toast.makeText(ChangePassword.this,
"Re-Enter New Password",
Toast.LENGTH_SHORT).show();
txtOldPass.setText("");
txtNewPass.setText("");
txtCnfPass.setText("");
} else {
try {
handler = new Handler();
handler.postDelayed(new Thread (new Runnable(){
JSONStringer loginuser = new JSONStringer()
.object()
.key("userid")
.value(SetGetValues.getUserid()
.trim())
.key("password")
.value(txtCnfPass.getText()
.toString().trim())
.key("oldpassword")
.value(txtOldPass.getText()
.toString().trim())
.endObject();
StringEntity entity = new StringEntity(
loginuser.toString());
JSONObject results = bc
.returnJSONObject(loginuser,
"url");
String message = results
.getString("message");
String isvalid = results
.getString("isvalid");
if (isvalid.contains("FALSE")) {
} else {
//pd.dismiss();
Toast.makeText(
ChangePassword.this,
"Password changed successfully",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
//TODO: handle exception
e.printStackTrace();
}
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}), 1000);
}
}
});
You are not dismissing the same ProgressDialog that you are showing. Replace this:
final ProgressDialog pd = new ProgressDialog(ChangePassword.this);
ProgressDialog.show(ChangePassword.this, "", "Loading...", false, true);
with this:
final ProgressDialog pd = ProgressDialog.show(ChangePassword.this, "", "Loading...", false, true);
if (pd.isShowing()) {
pd.dismiss();
}
Instead of using thread Use Async Task
usage
You problem probably is, that you're trying to modify the UI (dismiss the dialog) from another thread. I'm surprised that you don't get an error message. Try dismissing the dialog from the UI thread, for example by using .runOnUIThread().
Because you are trying to use element of UI Thread progressDialog in a non UI thread .
This will be allowed through AsyncTask , handler or RunOnUIThread(runnable) only .
read more about them and use any one .
progressDialog = ProgressDialog.show(GetResponse.this, "", "Loading...");
new Thread()
{
public void run()
{
try
{
// inside i have written code for making connection to the server using SSL connection.
}catch (Exception e)
{
progressDialog.dismiss();
exception(e.getMessage())
}.start();
}
private void exception(String msg)
{
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
this.finish();
Intent i = new Intent(getBaseContext(), LoginPage.class);
startActivity(i);
}
my LoginPage.java is previous activity.
If the connection is successfull it goes to the next activity ot doesnt give any error,
But if der is any prob with connection then i want progress bar should be stopped and go back to the LoginPage activity and also i want the error msg to be displayed.
From the above im getting some error.. Please help me out on this
Pass in and use the context from LoginPage. Also, use the 101010 button to format your code as code in your posts.
you can go up by using try catch mechanism where in your catch place your toast message and u can do it also by asynchronous task,
here simple code
private class Task_News_ArticleView extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(
Bru_Sports_View.this);
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
this.dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
try {
//here the condition to check login details
}
} catch (Exception e) {
}
return null;
}
protected void onPostExecute(Void result) {
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
}
}
and u can also use try,catch in catch block you can place your toast message
with finsih() method