Center text in a Toast

AndroidTextAlignmentCenterAndroid Toast

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

Solution 2 - Android

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

Solution 3 - Android

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?

Solution 4 - Android

It is dirty hack, but

((TextView)((LinearLayout)toast.getView()).getChildAt(0))
    .setGravity(Gravity.CENTER_HORIZONTAL);

Solution 5 - Android

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

Solution 6 - Android

Toast toast = Toast.makeText(this, "Message", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

Solution 7 - Android

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

Solution 8 - Android

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()
}

Solution 9 - Android

It is work for me:

Toast toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER| Gravity.BOTTOM, 0, 20);
toast.show();

Solution 10 - Android

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

Solution 11 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSonomanView Question on Stackoverflow
Solution 1 - AndroidMarcView Answer on Stackoverflow
Solution 2 - AndroidSephyView Answer on Stackoverflow
Solution 3 - AndroidYetAnotherUserView Answer on Stackoverflow
Solution 4 - AndroidMikhailView Answer on Stackoverflow
Solution 5 - AndroidSameer SegalView Answer on Stackoverflow
Solution 6 - AndroidserjView Answer on Stackoverflow
Solution 7 - AndroidimbrykView Answer on Stackoverflow
Solution 8 - Androidvishnu bennyView Answer on Stackoverflow
Solution 9 - AndroidKumar VLView Answer on Stackoverflow
Solution 10 - AndroidMuhamed HuseinbašićView Answer on Stackoverflow
Solution 11 - AndroidPintu NandesariyaView Answer on Stackoverflow