Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Set the text align to the middle of the toast [duplicate]

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?

like image 924
Milos Cuculovic Avatar asked Jan 18 '23 13:01

Milos Cuculovic


1 Answers

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

like image 174
Raoul George Avatar answered Mar 19 '23 07:03

Raoul George