displaying in android a timed custom dialog while pausing main thread execution - android

I am showing a custom dialog in my application, which stays for a small time, (say 2 seconds) and then disappears. I am calling this custom dialog from the main activity.
I want my calling activity to be paused till the custom dialog disappears. The issue is that activity code after the dialog is dismissed is always executed, while the dialog is being shown.
I have tried showing the dialog, sleeping for 2 seconds and then dismiss it, which is not working. The activity code after the dialog is dismissed is executing.
cust_dlg.show();
Thread.sleep(2000);
cust_dlg.dismiss();
I have also tried putting the dialog in a timer task, which also fails
final Timer t = new Timer();
cust_dlg.show();
t.schedule(new TimerTask() {
public void run() {
cust_dlg.dismiss();
t.cancel();
}
}, 500);
I have also tried simple threading with the custom dialog in a thread and put sleep simultaneously in the main activity, which causes exception and force closes.
Somebody please tell me a way to pause the main activity while the custom dialog is shown, so that the code after the custom dialog dismiss is NOT executed.
Thanks

Try a Handler
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(RBLQuizSpiel.this);
if (aktuellGeloest == true){
builder.setMessage(R.string.quiz_neu)
.setTitle(R.string.quiz_glueckwunsch)
.setPositiveButton(R.string.quiz_ja, dialogClickListener)
.setNegativeButton(R.string.quiz_nein, dialogClickListener).show();
} else {
builder.setMessage(R.string.quiz_neu)
.setTitle(R.string.quiz_schade)
.setPositiveButton(R.string.quiz_ja, dialogClickListener)
.setNegativeButton(R.string.quiz_nein, dialogClickListener).show();
}
}
}, 1500);
no more code behind this.
1500 = ms
Well an you need to set the execution of the following code in the Clicklistner.
Android is event-driven OS. The Dialog will be put in a message qeue and your program steps on..

Related

Android button disable till the Toast is seen

I have a button which shows a progress dialog, on end of progress dialog, a toast is shown.
I want the button to be diabled when the progress dialog and the toast are seen on the UI. i.e. after the toast is gone i want my button to be enabled again
Can anybody suggest what to do
As soon as you show the toast, set the button clickable to false, and start this timer task. The method of the class Timer namely schedule(), is such that it is executed after the provided time. In this case i passed the time as Toast.LENGTH _SHORT
final Handler handler = new Handler();
Timer time = new Timer();
time.schedule(new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
#Override
public void run() {
your_button.setClickable(true);
}
});
}
},Toast.LENGTH_SHORT); //// If your toast is for length short.
Put the below code before the progress dialog starts
Button myBtn=findViewById(R.id.button1);
myBtn.setVisibility(View.INVISIBLE);
//myBtn.setEnabled(false);
After Toast.makeText() is called, put the below code:
myBtn.setVisibility(View.VISIBLE);
//myBtn.setEnabled(true);
Note that setVisibility will make the button visible/invisible, setEnabled(false) will turn your button to non-clcikable mde.

Display a message on Android

Good afternoon,
How I can display a message for a few seconds without using Toast on Android?.
For example, if the user has logged well then I want to show a message like "User logged in successfully" disappears in X seconds.
How I can do?
thank you very much
final Handler handler = new Handler();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("FooBar !");
final AlertDialog dialog = builder.create();
dialog.show();
handler.postDelayed(new Runnable() {
public void run() {
dialog.dismiss();
}
}, 3000); // Dismiss in 3 Seconds
Alert dialog?
http://developer.android.com/reference/android/app/AlertDialog.html
try this...
while(needToDisplayData)
{
displayData(); // display the data
Thread.sleep(10000); // sleep for 10 seconds
}
Alternately you can use a Timer:
int delay = 1000; // delay for 1 sec.
int period = 10000; // repeat every 10 sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
displayData(); // display the data
}
}, delay, period);
in the displayData(); method u can use a dialog.
If you don't want an "OK" button to dismiss the message, you can put it up as a ProgressDialog and then dismiss it yourself after a couple of seconds like this...
ProgressDialog pd;
pd = ProgressDialog.show(context, "Title", "sub title");//context is probably `this`
Handler h= new Handler();
Runnable cancelDialog = new Runnable(){
pd.dismiss();
};
h.postDelayed(cancelDialog, 3000);//this will be called in 3 seconds
You can distribute the various calls to whatever methods or button presses are relevant. I'd make the ProgressDialog, the Handler and the Runnable global to your Activity so that you can make those calls from wherever.
I would argue that using the ProgressDialog gives the user a feeling that this is going to go away on its own, otherwise, they're left staring at a prompt that is not "dismissable" and confused as to how to proceed.
you can use PopupWindow to show massage just like Toast without blocking Current UI.but you will need to use a thread with runOnUiThread for dismiss PopupWindow after X Seconds.
See example-of-using-popupwindow
http://android-er.blogspot.in/2012/03/example-of-using-popupwindow.html

Auto-Close Alert Dialog

I have need where I have to auto close the Alert Dialog in 2 different situations
Auto-close the dialog after I get a return value which I am waiting for
Auto-close the dialog after 10 sec of no input from User. I know I should use timer of some sort, but not sure how to attach it with dialog.
I know and understand that its not right way to handle UI, but my requirement needs me to do this.
Please share your thoughts,
Thanks,
SKU
1- For the first case :
AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create();
...
alertDialog.show();
int valueIamWaitingFor = 5;
if (aValue == valueIamWaitingFor){
alertDialog.hide();
}
2- For the second case :
private static final ScheduledExecutorService executor =
Executors.newSingleThreadScheduledExecutor();
public AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create();
...
alertDialog.show();
Runnable hideDialog= new Runnable() {
public void run() {
this.alertDialog.hide();
}
};
executor.schedule(hideDialog, 10, TimeUnit.SECONDS);
Inherit a custom Dialog from AlertDialog, where you handle your timer in onStart. Using the AsyncTask would be good to do the countdown.
Auto-closing dialog ain't something bad... at least we always see this in changing screen resolution (win xp) which is good for such scenario. Maybe you can also include the countdown timer on the button (like "closing in 5 seconds").

Android dialog box

Is it possible to give a time limit in dialog box like a toast message. I want to display a set of strings in toast or dialog message box with button option. I used custom toast box previously, but i cant able to insert a button over the toast message. some of my friends suggested to implement dialog box instead of using Toast message. is there any possible to give Time limit in dialog box,(like Toast.long or shot.).
TimerTask would not be a good choice since you can not change the UI thread from TimerTask; Use Handler instead.... You can do this by using handler and runnable... simply use handler to call the runnable after some time. and in runnable simply dismiss the dialoge....
Handler h = new Handler();
h.postDelayed(runnable, delayMillis);
where runnable can be define as:
public Runnable r = new Runnable()
{
public void run()
{
// TODO Auto-generated method stub
}
};
create a dialog, then create a TimerTask and in the run method dismiss/cancel the dialog. Then create a Timer and schedule this task to be run after our desired time
when you call show() method after that you can start a counter after a certain interval when the counter condition is true then you can set the visibility of dialog box is false by calling dismiss() method.

Are progress/indeterminate dialog boxes added to the end of the UI thread? Do they require a second thread?

I have a simple dialog box. When I click a button the dialog box is supposed to be shown, while a file save operation is performed, and then the dialog box is dismissed. The problem I am having is the dialog box isn't shown until after the onClick event of the button finishes.
Taken from the Dialog developer doc:
The setup is simple. Most of the code
needed to create a progress dialog is
actually involved in the process that
updates it. You might find that it's
necessary to create a second thread in
your application for this work and
then report the progress back to the
Activity's UI thread with a Handler
object. If you're not familiar with
using additional threads with a
Handler, see the example Activity
below that uses a second thread to
increment a progress dialog managed by
the Activity.
Why isn't the dialog shown until after the onClick method finishes? Is the dialog added to the end of the UI thread?
Is the only way to do this to create a new thread and handler? That's fairly bad wording in the developer doc if so.
Thanks all.
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(SAVING_DIALOG);
//Do all the file saving operations
...
...
dismissDialog(SAVING_DIALOG);
}
});
Here is the dialog
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case SAVING_DIALOG: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Saving file...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
return dialog;
}
}
return null;
}
Shouldn't work be done on a thread when using the progress dialog?
http://www.helloandroid.com/tutorials/using-threads-and-progressdialog
For showing a progress dialog while something is processing, you have to process your code in another thread, otherwise your dialog freezes or wont be shown.
So I would use following method:
final Handler threadHandler = new Handler();
// in your onClick:
showDialog(SAVING_DIALOG);
new Thread(){
public void run(){
// new thread
// Do all the file saving operations
// ...
threadHandler.post(new Runnable(){public void run(){
// back in UI thread
dismissDialog(SAVING_DIALOG);
}});
}
}.start();

Categories

Resources