Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Toast Appear Length

Is there anyway I can tell a Toast Notification to show up only for a specified amount of time. Generally shorter then a regular toast message.

like image 990
Mitchell Avatar asked Sep 23 '10 02:09

Mitchell


People also ask

How do you set the length of toast?

makeText(context, "Hello world, I am a toast.", Toast. LENGTH_SHORT). show(); The duration for which a toast is displayed on screen is unfortunately defined by a flag: you can either show it for a SHORT duration, which is 2 seconds or a LONG duration which is 3,5 seconds.

How do you set a toast position?

Positioning your Toast A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.

Can we set a custom layout for a toast?

If a simple text message isn't enough, you can create a customized layout for your toast notification. To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView (View) method.

How do I show toast notifications?

Display the created Toast Message using the show() method of the Toast class. The code to show the Toast message: Toast. makeText(getApplicationContext(), "This a toast message", Toast.


1 Answers

I found a solution to this by calling toast.cancel() after a certain delay that is shorter than the standard toast duration.

        final Toast toast = Toast.makeText(ctx, "This message will disappear in 1 second", Toast.LENGTH_SHORT);         toast.show();          Handler handler = new Handler();             handler.postDelayed(new Runnable() {                @Override                public void run() {                    toast.cancel();                 }         }, 1000); 
like image 127
noypiscripter Avatar answered Sep 25 '22 12:09

noypiscripter