Reputation: 3610
I want to display toast less than Toast.LENGTH_SHORT, as i feel its taking around 2 seconds. i want to display toast only for half second.
And what is time interval for Toast.LENGTH_SHORT and Toast.LENGTH_LONG ?
Upvotes: 28
Views: 62165
Reputation: 11
public void showMyToast(final Toast toast, final int delay) {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
toast.show();
}
}, 0, 1000);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
toast.cancel();
timer.cancel();
}
}, delay);
}
//how to use(MainActivity.this is example ,it could be changed by yourself) and (In showMyToast(first param ,second param) method, second parameter is time what u really defined like this)
Toast toast=Toast.makeText(MainActivity.this, "MyToast Test", Toast.LENGTH_LONG);
showMyToast(toast, 1000);
Upvotes: 1
Reputation: 3869
This has worked for me
final Toast toast = Toast.makeText(getApplicationContext(), "The following message will disappear in half second", Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, 500);
Upvotes: 33
Reputation: 1086
For noobs I've made the simplest solution - a method.
public void showToastMessage(String text, int duration){
final Toast toast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
toast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
toast.cancel();
}
}, duration);
}
You should also import:
import android.os.Handler;
import android.widget.Toast;
You can call this method for example:
showToastMessage("your noob", 1000);
method above should work only in Fragment! If you want it to work in Activity, replace getActivity() with getApplicationContext() in toast message. Good luck developers!
Upvotes: 0
Reputation: 2062
Try this
final Toast toast = Toast.makeText(getBaseContext(), "YOUR MESSAGE",Toast.LENGTH_SHORT);
toast.show();
new CountDownTimer(10000, 1000)
{
public void onTick(long millisUntilFinished) {toast.show();}
public void onFinish() {toast.cancel();}
}.start();
Hope this help.. Enjoy..!!!
Upvotes: 3
Reputation: 20346
There are only two possible values:
private static final int LONG_DELAY = 3500; // 3.5 seconds
private static final int SHORT_DELAY = 2000; // 2 seconds
Setting other values doesn't work. If duration not equals 1 (Toast.LENGTH_LONG
), then duration will be SHORT_DELAY (2 seconds):
long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
In sources of Toast
written that
This time could be user-definable.
but I can't find way to do this.
Update: There is solution here: Set Toast Appear Length
Upvotes: 24
Reputation: 3869
it will work.
public void toastMessage(final String message) {
this.runOnUiThread(new Runnable() {
public void run() {
LayoutInflater myInflator = getLayoutInflater();
View myLayout = myInflator.inflate(R.layout.custom_layout,
(ViewGroup) findViewById(R.id.toastlayout));
TextView myMessage = (TextView) myLayout
.findViewById(R.id.label);
myMessage.setText(message);
Toast toast = new Toast(getApplicationContext());
toast.setView(myLayout);
toast.setDuration(100);
myMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL
| Gravity.CENTER_VERTICAL);
toast.show();
}
});
}
Upvotes: 0
Reputation: 1461
See my suggested solution here. You basically call toast.cancel() after a specified delay that is shorter than the standard toast duration.
Upvotes: 10
Reputation: 11728
This seems to work for me (set the duration to whatever you want):
mActivity.runOnUiThread(new Runnable() {
public void run() {
Toast toast = new Toast(mActivity
.getApplicationContext());
toast.setView(layout);
toast.setDuration(400);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
});
Upvotes: -4