views:

141

answers:

2

Hi guys,

I was wondering if there was a way to display all text in a toast to be centered. For instance, I have a toast that has 2 lines of text in it. For purely aesthetic reasons, I would like the text to center-aligned instead of left-aligned. I've looked through the documentation and can't find anything about it. Is there a simple way to do this that I have missed?

Thanks Chris

+1  A: 

Use the Toast's setView(view) function to supply a View with Gravity.CENTER

Sameer Segal
+2  A: 

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);
Sephy