How to show Progress Dialog Button dynamically after n seconds delay - android

How to show the ProgressDialog button after n seconds (fixed) delay that the ProgressDialog is shown?
To be more clear, the ProgressDialog starts normally. How can I show a button within it after n seconds?
L.F.
EDIT
Using the answers of Segi and android_beginner (thank you very much) I'm posting the solution of my problem:
pDialog = new ProgressDialog(mContext);
pDialog.setTitle(Reso.getString(mContext, R.string.waiting));
pDialog.setMessage(Reso.getString(mContext, R.string.waiting));
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.setButton(DialogInterface.BUTTON_NEGATIVE,
Reso.getString(mContext, R.string.annulla),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Stuff
}
});
pDialog.show();
pDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setVisibility(View.INVISIBLE);
int progressDialogCancelButtonDelay = 2500;
new CountDownTimer(progressDialogCancelButtonDelay, progressDialogCancelButtonDelay + 1) {
#Override public void onTick(long millisUntilFinished) { }
#Override public void onFinish() {
pDialog.getButton(ProgressDialog.BUTTON_NEGATIVE).setVisibility(View.VISIBLE);
}
}.start();

Try Something Like Below Code:
ProgressDialog loadingDialog = ProgressDialog.show(activity.this, "", "Loading...",
true, false);
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(2500);
runOnUiThread(new Runnable() {
public void run() {
loadingDialog.dismiss();
loadingDialog.setButton(ProgressDialog.BUTTON_NEUTRAL,
"Close",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//button click stuff here
}
});
loadingDialog.show();
loadingDialog.getButton(ProgressDialog.BUTTON_NEUTRAL).setVisibility(View.INVISIBLE);
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();

Just extend this class instead of AsyncTask and be sure to call super() when appropriate:
public abstract class AsyncTaskWithDelayedIndeterminateProgress
<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
private static final int MIN_DELAY = 250;
private final ProgressDialog progressDialog;
private final CountDownTimer countDownTimer;
protected AsyncTaskWithDelayedIndeterminateProgress(Activity activity) {
progressDialog = createProgressDialog(activity);
countDownTimer = createCountDownTimer();
}
#Override protected void onPreExecute() {
countDownTimer.start();
}
#Override protected void onPostExecute(Result children) {
countDownTimer.cancel();
if(progressDialog.isShowing())
progressDialog.dismiss();
}
private ProgressDialog createProgressDialog(Activity activity) {
final ProgressDialog progressDialog = new ProgressDialog(activity);
progressDialog.setIndeterminate(true);
return progressDialog;
}
private CountDownTimer createCountDownTimer() {
return new CountDownTimer(MIN_DELAY, MIN_DELAY + 1) {
#Override public void onTick(long millisUntilFinished) { }
#Override public void onFinish() {
progressDialog.show();
}
};
}

make a layout with your ProgressDialog and an invisible button on it.
When you show the dialog activate a timer, that set the button visible after n seconds.
I dont know if i understand your question right, but i hope so. =)

Related

Is there a way to show AlertDialog in AsyncTask commonly?

I want to show AlertDialog which is in other class in AsyncTask.
Example>
public class WardListAsyncTask extends AsyncTask<HashMap<String, String>, String, Object> {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Object doInBackground(HashMap<String, String>... params) {
...
}
#Override
protected void onPostExecute(Object result) {
ConfirmAlertDialog(ez_WardList.this,"HI?");
//this method is in another Class & I want to use this method another Asynctask also..
}
and ConfirmAlertDialog is...
Context g_ctx;
String g_content;
public void ez_ConfirmAlertDialog(Context ctx, String content) {
this.g_ctx=ctx;
this.g_content=content;
runOnUiThread(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT);
builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(displayWidth / 2, LayoutParams.WRAP_CONTENT);
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239));
}
});
}
I think g_ctx.class.runonuiThread ... but I can't call runonuithread...
How can I solve it?
runOnUiThread method is present in Activity class. So should pass Activity into your class.
Example:
public class MyClass{
public void ez_ConfirmAlertDialog(Activity activity, String content) {
this.g_ctx=ctx;
this.g_content=content;
if(activity == null){
return;
}
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(g_ctx, AlertDialog.THEME_HOLO_LIGHT);
builder.setMessage(g_content).setPositiveButton(getString(R.string.kor_confirm),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(displayWidth / 2, LayoutParams.WRAP_CONTENT);
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(Color.rgb(10, 174, 239));
}
});
}
}
You can call it in main thread using handler
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
// Your code here
}
});

Set Progress Dialog properly in asynctask

I currently trying to show a progress dialog on OnclickListener of a Dialogbox since my items are taking too long to fetch from Server.
I use Async task as suggested here (Android progress dialog) and this post (android problem with progress dialog) to show progress dialog The progress dialog is shown , however the code returns exception when it goes to do background that " Looper is not set". And when I set looper nothing happens.
I am not sure at this stage what is it that I am doing wrong.
public void firstMethod()
{
final CustomObj obj = getCustomObj();//not imp
Messages.getInstance().showAlert(MainActivity.this, "message", false, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
}, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int which)
{
gotoAnotherPge(obj);
}
});
}
public void gotoAnotherPge(final CustomObject obj)
{
final ProgressDialog pd = new ProgressDialog(MainActivity.this);
new AsyncTask<Object, Object, Boolean>()
{
protected void onPreExecute()
{
pd.setMessage(String.format(Statics.getText(MainActivity.this, R.raw.dictionary, "subscriptions_loading_unsubscribing")));
pd.show();
}
protected Boolean doInBackground(Object... params)
{
try{
Looper.prepare();
final LocalPopulator lp = new LocalPopulator(MainActivity.this, 0)
{
#Override
public void populate()
{
List<Serializable> items = Arrays.asList(getItemHere(obj));
List<Serializable> listItems = new ArrayList<Serializable>();
listItems.addAll(items);
Serializable[] sItems = listItems.toArray(new Serializable[menuItems.size()]);
result = sItems;
}
};
showNextPage(true, 1, 0, lp);
Looper.loop();
}catch (Exception e){
Log.e("tag", e.getMessage());
/*
* The task failed
*/
return false;
}
return true;
}
protected void onPostExecute(Boolean result)
{
pd.dismiss();
}
};
MainActivity.this.runOnUiThread (new Runnable()
{
#Override
public void run()
{
// dismiss the progressdialog
pd.dismiss();
}
});
}

Show progressDialog in Android

I want to show a progressDialog on my page when the user clicks on the button.On click of button i am sorting my results that is a List.Now how can we show a progressDialog on click of a button.Please suggest me
This function i am using to sort that data :
public void sortByDate(View v) {
Collections.sort(tripParseData.getDetails());
setData(tripParseData);
}
After #Monica Suggestion
public void sortByDate(View v) {
new LoadData().execute();
}
class LoadData extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
Collections.sort(tripParseData.getCoroprateBookingDetails());
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
setApprovalDetailsData(tripParseData);
}
}
you have to call progressdiolog.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();
}
};
}
Don't forget to vote me up :)
Paste This Class in ur activity and call new LoadData().execute to start the prgress dialog :
class LoadData extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog pg=new ProgressDialog(ChartsActivity.this);
pg=pg.show(ChartsActivity.this, "Loding", "Plz Wait...");
}
#Override
protected Void doInBackground(Void... params) {
sortByDate();
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pg.dismiss();
}
}
I have not tested it but I think it can help you.
Declare these two objects in your class and member variables.
Thread thread = null;
ProgressDialog bar = null;
Use this logic on your button click listener it can work in your case. I have not tested it but It will give you Idea how things should work. you can also use handlemessage in place of runonuithread
if (thread == null
|| (thread != null && !thread.isAlive())) {
thread = new Thread(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
//DISPLAY YOUR PROGRESS BAR HERE.
bar = ProgressDialog.show(getApplicationContext(), "LOADING..", "PLEASE WAIT..");
}
});
// SORT COLLECTION HERE
runOnUiThread(new Runnable() {
#Override
public void run() {
//CLOSE YOUR PROGRESS BAR HERE.
bar.dismiss();
}
});
}
});
thread.start();
}
Hope this will help you.
try to use AsyncTask http://developer.android.com/reference/android/os/AsyncTask.html
Sample code:
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
//Show UI
//Start your progress bar
showProgress();
}
#Override
protected Void doInBackground(Void... arg0) {
// do your sorting process
return null;
}
#Override
protected void onPostExecute(Void result) {
//Show UI
//dismiss your progress bar
hideProgress();
}
};
task.execute((Void[])null);
Show and hide progress code
public void showProgress() {
progressDialog = ProgressDialog.show(this, "",
"Loading. Please wait...");
progressDialog.setCancelable(false);
}
public void hideProgress() {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}

add progress bar when one activity loads other activity

This is my activity oncreate() method. I set a positive button ok with a dialogue window.
when I click on it, it navigates UI activity staticDisplay.class. I want to set a progress bar after clicking ok in the dialogue window until it loads the next activity.
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "ACTIVITY ONCREATE");
super.onCreate(savedInstanceState);
// setContentView(R.layout.dialog);
setContentView(R.layout.main);
if (Registration.isRunning == false) {
TextView title = new TextView(this);
title.setText("DM2");
title.setBackgroundColor(Color.DKGRAY);
title.setPadding(10, 10, 10, 10);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(20);
/* alert message */
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCustomTitle(title);
builder.setMessage(R.string.app_description).setPositiveButton(
"Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (Registration.isRunning == false) {
startService(new Intent(
getApplicationContext(),
Registration.class));
}
staticInfo();
if (Registration.ruid == null)
Registration.ruid = uId;
startActivity(new Intent(getApplicationContext(),
StatisticDisplay.class));
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
startActivity(new Intent(getApplicationContext(),
StatisticDisplay.class));
}
}
StaticDisplay.class on create method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statdisplay);
usage_btn = (Button) findViewById(R.id.appstat);
usage_btn.setOnClickListener(this);
setting_btn = (Button) findViewById(R.id.setting);
setting_btn.setOnClickListener(this);
}
you can use AysncTask for background process to be done and showing progressdialog as follows,
call the class
new Task().execute(CurrentActivity.this);
In Task Class:
private class Task extends AsyncTask<Void, Void, Void>
{
ProgressDialog dialog;
Context context;
public Task(Context context)
{
this.context=context;
//constructor for this class
}
protected void onPreExecute() {
//create the progress dialog as
dialog=new ProgressDialog(context);
}
protected Void doInBackground(Void... JSONArray) {
//Place your background process code
}
protected void onPostExecute(Void unused) {
//dismiss the progressdialog
dialog.dismiss();
}
}
This Code , Working For me
private ProgressDialog progress;
Button CallButton;
#Override
protected void onCreate(Bundle savedInstanceState)
{
progress = new ProgressDialog(Page.this);
CallButton=(Button)findViewById(R.id.button1);
CallButton.setOnClickListener(this);
}
#Override
public void onClick(View v)
{
switch (v.getId()) {
case R.id.button1:
progress.setMessage("Please Wait Loading...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
new Thread()
{
public void run()
{
Intent i = new Intent(Page.this,Update.class);
startActivity(i);
progress.dismiss();
}
}.start();
break;
}
}
Please look at to following code snippet,
progDailog = ProgressDialog.show(loginAct,"Process ", "please wait....",true,true);
new Thread ( new Runnable()
{
public void run()
{
// your loading code goes here
}
}).start();
Handler progressHandler = new Handler()
{
public void handleMessage(Message msg1)
{
progDailog.dismiss();
}
}
In the run() method, you can put your loading code.

problem to show progress bar

i want to show spinning wheel progress but it doesn't show anything.
final ProgressDialog dialog = ProgressDialog.show(AppListActivity.this,
"", "Loding...", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
// Set our custom array adapter as the ListView's adapter.
listAdapter = new AppArrayAdapter(AppListActivity.this,
appsList);
mainListView.setAdapter(listAdapter);
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
please figure out this one..
thanks..
you haven't written dialog.show()..
refer to this code
ProgressDialog UpdateDialog = new ProgressDialog(ClassContext);
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
UpdateDialog.setTitle("MyApp");
UpdateDialog.setMessage("Processing data...");
UpdateDialog.show();
super.onPreExecute();
}
#Override
protected Object doInBackground(Object... params) {
//your code
}
#Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
//your code
UpdateDialog.dismiss();
}
ProgressDialog mPrograss=new ProgressDialog(location.this);
mPrograss.setTitle("loading....");
mPrograss.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mPrograss.setMax(1);
mPrograss.setProgress(2000);
mPrograss.setButton("save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
mPrograss.setButton2("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
mPrograss.show();
i think u can use the code below.
private ProgressDialog pd;
pd = ProgressDialog.show(context, "Please wait...", "Saving to database...", true,false);
.
pd.dismiss();

Categories

Resources