Android Set the text in the middle of the toast

I have a toast message that is quite long. I would like to set the text in the middle and not start aligning to the left.

Is it possible?

+4
source share
2 answers

Toast is built on TextView, and by default, gravity is aligned to the left. So you need to create your own TextView, for example:

<TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal" android:text="all the text you want" /> 

And you assign TextView to Toast as follows:

 Toast t = new Toast(yourContext); t.setView(yourNewTextView); 

Source

+6
source

Here:

 Toast toast = Toast.makeText(this, "Message", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); 
+4
source

Source: https://habr.com/ru/post/1395216/


All Articles