4

I have a toast message which is pretty long. I would like to set the text in the middle and not to start align to the left.

Is this possible?

Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146
Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

2 Answers2

6

Toast is built on a TextView and the default gravity of it is left aligned. So, you need to create your own TextView like this for instance :

<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 the TextView to the Toast like this :

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

Source

Community
  • 1
  • 1
Raoul George
  • 2,807
  • 1
  • 21
  • 25
3

Here:

Toast toast = Toast.makeText(this, "Message", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Dmytro Danylyk
  • 19,684
  • 11
  • 62
  • 68