I am developing an application where I have to use numerous toasts.
I display these toasts by using:
Toast.makeText(context, "Some medium-sized text", Toast.LENGTH_SHORT).show();
The displayer toast, however, has the height of one line, while the text is displayed on multiple lines. The result is that I can't view all the text in the toast.
How can I fix this?
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. Notice that the ID of the LinearLayout element is "toast_layout".
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.
Try inserting a carriage-return and line-feed where you want to split the text.
These characters refer back to the old typewriter models. Carriage return was the cylinder moving back to the start and line feed was the cylinder rolling (feeding) by one line.
In computing these are represented by two escaped characters (special codes that allow non-printable codes inside a string by prefixing them with a backslash \
).
\r
\n
(you can remember this as a new line).Some non-unix systems (e.g. Windows) require both, others (e.g Linux on which Android is based) only need the new line but it is generally safe to do both everywhere. The one thing that is essential is the order they are in. It must be \r\n
To put this into your example:
Toast.makeText(context, "First line of text\r\nSecond line of text", Toast.LENGTH_SHORT).show();
In Android you should be able to reduce this to just the new line character \n
as unix based systems are not so fussy:
Toast.makeText(context, "First line of text\nSecond line of text", Toast.LENGTH_SHORT).show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With