Center text in a toast in Android

Sonoman picture Sonoman · Aug 19, 2010 · Viewed 42.6k times · Source

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

Answer

Marc picture Marc · Nov 21, 2012

Adapted from another answer:

Toast toast = Toast.makeText(this, "Centered\nmessage", Toast.LENGTH_SHORT);
TextView v = (TextView) toast.getView().findViewById(android.R.id.message);
if( v != null) v.setGravity(Gravity.CENTER);
toast.show();