android toast doesn't fit text

Gabriel picture Gabriel · Jul 31, 2011 · Viewed 11k times · Source

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?

Answer

Moog picture Moog · Jul 31, 2011

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 \).

  • A carriage return is represented by \r
  • A line-feed is represented by \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();