Android dialog box - android

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.

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

How to close AlertDialog.Builder without clicking any button

AlertDialog.Builder dialog = new AlertDialog.Builder(this);
ListDialog.setPositiveButton(R.string.str_choose, new YesListener());
ListDialog.setNegativeButton(R.string.str_cancel, new NoListener());
dialog.show();
Above code is mine.
I can dismiss dialog by click button.
But if I want to dismiss it without click any button.
How can I do it?
Use a handler to delay the call to Dialog.dismiss(). Here's an example.
Define the appropriate instance variables in your Activity:
// number of milliseconds to wait (2 seconds, in this example).
public static final int DELAYED_RESPONSE = 2000;
// define a handler as a private instance variable in your Activity.
private Handler handler = new Handler();
Use the following code to post a new Runnable that will dismiss your dialog after DELAYED_REPONSE milliseconds:
handler.postDelayed(new Runnable() {
#Override
public void run() {
dismissDialog();
}
}, DELAYED_RESPONSE);
Note that this code assumes that your Activity implements a method dismissDialog() that will dismiss your dialog accordingly.
The best way to do it would be to create a Handler on your UI threads looper and then post a delayed Message or Runnable that executes Dialog.dismiss().
You can use set setCancelable(true) by doing this you can press BACK button of the device.
Example:
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setCancelable(true).show();

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

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..

How do I cause the timer and compare methods to execute before my Android toast goes away?

/* In onclick function */
Toast.makeText(this,"you are right!",Toast.LENGTH_SHORT).show();
timer();
compare();
/* onclick function ends */
In this snippet when the dialog is popping up, the timer and compare methods are executed before the dialog goes away. How do I fix this? Can someone explain with a code snippet?
Also, I want the LENGTH_SHORT to be 400ms. How can I change that?
How can I do this simple task using alertdialog?
You can give time in miliSecond in Toast
Toast.make(this,"Hello",400).show();
now// take one thread
Thread th=new Thread(new Runnable(){
try{
th.sleep(400);
timer();
compare();
}
catch(Exception e){
e.getExption();
}
});

Categories

Resources