Reputation: 260
I am new in android, I want see my message in a Toast
but it shows to for a little time.
I want to show the message for example until one hour or more.
Upvotes: 0
Views: 1530
Reputation: 8818
Sets toast to a specific period in milli-seconds:
public void toast(int millisec, String msg) {
Handler handler = null;
final Toast[] toasts = new Toast[1];
for(int i = 0; i < millisec; i+=2000) {
toasts[0] = Toast.makeText(this, msg, Toast.LENGTH_SHORT);
toasts[0].show();
if(handler == null) {
handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toasts[0].cancel();
}
}, millisec);
}
}
}
Upvotes: 0
Reputation: 133560
No you can't. Use a Custom Dialog and dismiss it when you want. But i wonder why do you want to display some kind of pop up for such a long time.
I would suggest re-considering your design.
You may also want to check Crouton
https://github.com/keyboardsurfer/Crouton
Upvotes: 1
Reputation: 715
The purpose of toast is showing a simple message in a time. You can't show it for long time. you can customized your own UI for Toast messages using dialog.
public static void showCustomToast(final Activity mActivity,final String helpText,final int sec) {
if(mActivity != null){
mActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
int mSec = 3000;
if(sec != 0){
mSec = sec;
}
LayoutInflater inflater = mActivity.getLayoutInflater();
View messageDialog = inflater.inflate(R.layout.overlay_message, null);
layer = new CustomLayout(mActivity);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
messageDialog.setLayoutParams(params);
TextView message = (TextView) messageDialog.findViewById(R.id.messageView);
Button okBtn = (Button) messageDialog.findViewById(R.id.messageOkbtn);
if(okBtn != null){
okBtn.setVisibility(View.GONE);
}
message.setText(helpText);
final Dialog dialog = new Dialog(mActivity,R.style.ThemeDialogCustom);
dialog.setContentView(messageDialog);
dialog.show();
final Timer t = new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
if(dialog.isShowing()){
dialog.dismiss();
}
t.cancel();
}
},mSec);
}
});
}
}
Upvotes: 0
Reputation: 1859
Well, like said here, there is no proper way to do this.
But, there is a sort of hack to it - just run your Toast in a for-loop
, and the amount of iterations will control the length. For example - running the loop twice (like below) will double the time. Running it 3 times will triple the length. Again, it is just a work-around that works :-)
for (int i=0; i < 2; i++)
{
Toast.makeText(this, "test", Toast.LENGTH_LONG).show();
}
You must take into account that it does have flaws - it the user quits the app before the end of loop it will continue to show, and, on some devices the Toast might flicker between each iteration. So, up to you!
Upvotes: 0
Reputation: 4683
You can try this :
Edit:
int time = 1000*60 // 1 hour
for (int i=0; i < time; i++)
{
Toast.makeText(this, "Your msg", Toast.LENGTH_LONG).show();
}
Upvotes: 0
Reputation: 5068
The values of LENGTH_SHORT and LENGTH_LONG
are 0 and 1
.they are treated as flags therefore I think it is not possible to set time other than this.
Upvotes: 0
Reputation: 2365
Try to use Dialog box instead of toast
SingleButtton.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Creating alert Dialog with one Button
AlertDialog alertDialog = new AlertDialog.Builder(AlertDialogActivity.this).create();
// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to Android Application");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
// Write your code here to execute after dialog closed
}
});
// Showing Alert Message
alertDialog.show();
}
});
Upvotes: 0