Center text in a Toast
Asked Answered
S

11

71

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?

Skill answered 19/8, 2010 at 13:3 Comment(0)
G
15

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

Gomorrah answered 19/8, 2010 at 13:20 Comment(1)
What about APIs < 23? Does it matter?Glorification
T
112

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();
Twombly answered 21/11, 2012 at 12:1 Comment(3)
Why the default gravity is not Gravity.CENTER? Silly android!Payment
I don't know why but I have failed this method on Android 11 (API-30).Metrology
I haven't worked with Android for years, it's possible that Google disabled this possibility?Twombly
D
28

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);
Dome answered 19/8, 2010 at 13:28 Comment(0)
M
28

Without the hacks:

String text = "Some text";
Spannable centeredText = new SpannableString(text);
centeredText.setSpan(
    new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
    0, text.length() - 1,
    Spannable.SPAN_INCLUSIVE_INCLUSIVE
);

Toast.makeText(getActivity(), centeredText, Toast.LENGTH_LONG).show();

There are also another alignments besides center.

cf. Multiple alignment in TextView?

Malchus answered 15/1, 2015 at 21:16 Comment(1)
It still works on Android 11 (API-30).Metrology
K
19

It is dirty hack, but

((TextView)((LinearLayout)toast.getView()).getChildAt(0))
    .setGravity(Gravity.CENTER_HORIZONTAL);
Koren answered 1/5, 2011 at 8:35 Comment(3)
Great! Make sure you surround with a ClassCastException catch to be safe in case the view hierarchy of Toasts changes in the future.Mcclung
This works on my Nexus 4. I suggest doing this, wrapping it with a ClassCastException as Max Howell said above and then live with it when it throws an actual exception.Shortfall
use findViewById(android.R.id.message) on the view returned by toast.getView() to ensure working code on all versionsGuile
G
15

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

Gomorrah answered 19/8, 2010 at 13:20 Comment(1)
What about APIs < 23? Does it matter?Glorification
E
13
Toast toast = Toast.makeText(this, "Message", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Earpiece answered 7/5, 2011 at 15:10 Comment(1)
This does not center the text, but the toast's itself.Inwrap
J
5

Not saing that findViewById(android.R.id.message) is wrong, but just in case there are (future?) implementation differences I myself used a bit differen approach:

void centerText(View view) {
    if( view instanceof TextView){
        ((TextView) view).setGravity(Gravity.CENTER);
    }else if( view instanceof ViewGroup){
        ViewGroup group = (ViewGroup) view;
        int n = group.getChildCount();
        for( int i = 0; i<n; i++ ){
            centerText(group.getChildAt(i));
        }
    }
}

and then:

Toast t = Toast.makeText(context, msg,Toast.LENGTH_SHORT);
centerText(t.getView());
t.show();
Jostle answered 24/4, 2014 at 10:18 Comment(0)
U
3

In kotlin:

fun makeToast(context: Context, resId: Int) {
    val toast = Toast.makeText(context, resId, Toast.LENGTH_SHORT)
    val view = toast.view.findViewById<TextView>(android.R.id.message)
    view?.let {
        view.gravity = Gravity.CENTER
    }
    toast.show()
}
Uncanonical answered 6/6, 2018 at 6:20 Comment(0)
J
1

It is work for me:

Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER| Gravity.BOTTOM, 0, 20);
toast.show();
John answered 13/3, 2018 at 6:53 Comment(1)
This does not work for me. The text is still aligned to the left.Cass
K
0

This variation is with using LinearLayout in your layout xml :)

Toast SampleToast = Toast.makeText(this, "This is the example of centered text.\nIt is multiline text.", Toast.LENGTH_SHORT);
LinearLayout OurLayout = (LinearLayout) SampleToast.getView();
    
if (OurLayout.getChildCount() > 0) {
   TextView SampleView = (TextView) OurLayout.getChildAt(0);
   SampleView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
}

SampleToast.show();
Kraut answered 2/2, 2015 at 10:41 Comment(1)
This is a hacky solution that might not work in future versions of android, for example if the first child is not a TextView. Does not follow Java coding standards.Nipping
P
-3
Toast t=Toast.makeText(getApplicationContext(),"Text",Toast.LENGTH_LONG);
t.setText("Password Does't match...");
t.setGravity(0, 0, 0);
t.show();

simple code for toast most be center

Plumbum answered 3/2, 2016 at 19:37 Comment(2)
Welcome to SO. Please look through the help page on answering questions.. As written, your code is poorly formatted and does not contain an explanation.Wiggler
Sorry, but your code set gravity center to the toast (it appear in center screen), not to the textFarr

© 2022 - 2024 — McMap. All rights reserved.