how to set text new line in Toast at center android
Asked Answered
S

3

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

Sparkie answered 24/5, 2016 at 18:34 Comment(1)
<string name="my_tring_test">aaaaaaaaaaaa\nbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\ncccccccccccccccc</string> Toast.makeText(getApplicationContext(), getResources().getString(R.string.my_string_test), Toast.LENGTH_LONG).show();Sparkie
A
12

\n inside the string works!!!

Toast.makeText(this, "line1\nLine2", Toast.LENGTH_SHORT).show();
Aun answered 6/2, 2017 at 9:45 Comment(1)
It works to add a new line but it does not center the text as OP asked.Houser
V
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

Voluntaryism answered 25/5, 2016 at 5:8 Comment(0)
S
2
Toast.makeText(this,"YOUR MESSAGE",
                      +"\n"+
                     "Your other message",

Toast.Lenght_long).show();

Each time you add a line with

+"\n"+

Subcortex answered 1/8, 2019 at 7:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.