Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android toast doesn't fit text

Tags:

android

toast

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?

like image 764
Gabriel Avatar asked Jul 31 '11 09:07

Gabriel


People also ask

How do I customize a toast on Android?

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".

Can we change the position of toast message in Android?

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.


1 Answers

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();
like image 71
Moog Avatar answered Oct 27 '22 13:10

Moog