9

My Toast have 3 lines, I want line 2 and 3 show center of this toast and set duration to 10 seconds.

How can do it?

Like this picture:

enter image description here

gonz
  • 5,226
  • 5
  • 39
  • 54
Noisyman
  • 119
  • 1
  • 1
  • 7
  • aaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\ncccccccccccccccc Toast.makeText(getApplicationContext(), getResources().getString(R.string.my_string_test), Toast.LENGTH_LONG).show(); – Noisyman May 25 '16 at 03:13

3 Answers3

12

\n inside the string works!!!

Toast.makeText(this, "line1\nLine2", Toast.LENGTH_SHORT).show();
Shlomi Fresko
  • 909
  • 11
  • 15
  • 2
    It works to add a new line but it does not center the text as OP asked. –  May 17 '17 at 18:17
5

You can customize Toast class. Here is example:

Context context=getApplicationContext();
        LayoutInflater inflater=getLayoutInflater();

        View customToastroot =inflater.inflate(R.layout.YOUR_TOAST_LAYOUT, null);

        Toast customtoast=new Toast(context);

        customtoast.setView(customToastroot);
        customtoast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL,0, 0);
        customtoast.setDuration(Toast.LENGTH_LONG);
        customtoast.show();

For more details tutorials visit here and here

Md. Sajedul Karim
  • 6,749
  • 3
  • 61
  • 87
2
Toast.makeText(this,"YOUR MESSAGE",
                      +"\n"+
                     "Your other message",

Toast.Lenght_long).show();

Each time you add a line with

+"\n"+

Muldec
  • 4,641
  • 1
  • 25
  • 44
Georges
  • 21
  • 1