I want to delay the toast on selected delay times like (15, 30, 60 seconds and no delay) but it won't work. Here's the code:
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
if(FirstLoad){
FirstLoad = false;
return;
}
Toast.makeText(parent.getContext(), "You chose " +
parent.getItemAtPosition(pos).toString()+ " to delay", Toast.LENGTH_LONG).show();
Message message = new Message();
Bundle bun = new Bundle();
bun.putString("delay", parent.getItemAtPosition(pos).toString());
message.obj = bun;
if (pos == 0) {
handler.sendMessageDelayed(message, 0);
}
else if (pos == 1) {
handler.sendMessageDelayed(message, 15000);
}
else if (pos == 2) {
handler.sendMessageDelayed(message, 30000);
}
else if (pos == 3) {
handler.sendMessageDelayed(message, 60000);
}
//handler.sendMessageDelayed(message, 15000);
}
public void onNothingSelected(AdapterView<?> parent) {
return;
}
Help Please.
Try this :
final Toast toast = Toast.makeText(parent.getContext(), "You chose "
+ parent.getItemAtPosition(pos).toString() + " to delay",
Toast.LENGTH_LONG);
Runnable showToastRunnable = new Runnable() {
public void run() {
toast.show();
}
};
if (pos == 0) {
handler.postDelayed(showToastRunnable, 0);
} else if (pos == 1) {
handler.postDelayed(showToastRunnable, 15000);
} else if (pos == 2) {
handler.postDelayed(showToastRunnable, 30000);
} else if (pos == 3) {
handler.postDelayed(showToastRunnable, 60000);
}
Edit:
By the way, I want to transfer this to the send button, i want to delay the toast of "Message sent" according to the delay the user chose. How should I implement it?
How are you fetching the delay? Is it something the user enters in an EditText
?
In that case you could just get the delay like this :
int delay = Integer.parseInt(delayEditText.getText().toString());
and then use that delay amount to post the runnable to the handler like this :
handler.postDelayed(showToastRunnable, delay);
You can remove your entire if-else block in this case.
for this you can use custom dialog and hide it after a particular time.
class CustomDialog extends Dialog
{
setContentView(R.layout.dialogxml);
txtview=(TextView)findViewById(R.id.txtmsg);
}
Customdialog dialog= CustomDialog.show();
dialog.hide();
Handler hl_DelayedToast = new Handler(); // scope global
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
if(FirstLoad)
{
FirstLoad = false;
return;
}
//if else logic to check the time
// if 0
hl_DelayedToast.postDelayed(mytoastshower,0);
// if 1
hl_DelayedToast.postDelayed(mytoastshower,1000);
}
public Runnable mytoastshower = new Runnable
{
public void run()
{
Toast.show();// show the toast
}
}
hope it helps.
Declare your handler this way:
Hanlder handlder=new Handler() {
public void handleMessage (Message msg) {
Toast.makeText(YOUR_ACTIVITY_CLASS_NAME.this,"You chose"+(Bundle(msg.obj)).getString("delay","DEFAULT_VALUE")+"to delay",Toast.LENGTH_LONG).show();
}
};
Simply, you don't have to use a bundle, but you can call msg.what=THE DELEY TIME.
Also, you can call handler.obtainMessage to get a message. See http://developer.android.com/reference/android/os/Handler.html#obtainMessage%28%29
So every time you send a message, it will be handled here, and thus you call show the toast.
Sorry that I don't have Eclipse installed on this laptop, so I can not test the code. However, I believe it works.