46

Is there any way to increase the font size in toast without customizing?

I don't want to create a layout for increasing the text size.

Is there any way?

Thanks,

Niki

Niki
  • 1,161
  • 1
  • 20
  • 37

9 Answers9

62

I believe it is achieveable by this:

    ViewGroup group = (ViewGroup) toast.getView();
    TextView messageTextView = (TextView) group.getChildAt(0);
    messageTextView.setTextSize(25);
slezadav
  • 6,104
  • 7
  • 40
  • 61
  • 1
    This code crashes due to ClassCastException in some custom OS devices. java.lang.ClassCastException android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout Seems toast view is implemented with a RelativeLayout parent in this cases. May be fixed by typecasting to ViewGroup instead of LinearLayout. – Mihir Shah Oct 07 '15 at 08:51
28

this is ...

 Toast toast = Toast.makeText(context, R.string.yummyToast, Toast.LENGTH_SHORT);
//the default toast view group is a relativelayout
RelativeLayout toastLayout = (RelativeLayout) toast.getView();
TextView toastTV = (TextView) toastLayout.getChildAt(0);
toastTV.setTextSize(30);
toast.show();
suku
  • 10,507
  • 16
  • 75
  • 120
Anil M H
  • 3,332
  • 5
  • 34
  • 51
24

Here is how to do that with spans:

SpannableStringBuilder biggerText = new SpannableStringBuilder(text);
biggerText.setSpan(new RelativeSizeSpan(1.35f), 0, text.length(), 0);
Toast.makeText(context, biggerText, Toast.LENGTH_LONG).show();
Nikolay Chorniy
  • 341
  • 2
  • 2
8

You can't increase the font size without creating a CustomToastView.

This is a related question.

Community
  • 1
  • 1
Wroclai
  • 26,835
  • 7
  • 76
  • 67
  • Thanks.. But even CustomToastView is not working in my Service.Where should we do the inflating part (onCreate or onStartCommand )? I tried in onCreate, but ended with error. – Niki Mar 12 '11 at 06:18
2

Working from Ani's answer, another solution that allows you to set the text size to a dimension value would be something like:

public static void showToast(Context context, int resId) {
    Toast toast = Toast.makeText(context, resId, Toast.LENGTH_LONG);
    LinearLayout toastLayout = (LinearLayout) toast.getView();
    TextView toastTV = (TextView) toastLayout.getChildAt(0);
    toastTV.setTextSize(TypedValue.COMPLEX_UNIT_PX,
            context.getResources().getDimension(R.dimen.TEXT_SIZE));
    toast.show();
}

This lets you match the size you get your toasts to be the same size as specified in TextView and Button controls, for example.

John Lockwood
  • 3,787
  • 29
  • 27
1

Nikolay answered this beautifully. Heuristically, it's pretty a safe bet that, if an Android API method takes a CharSequence instead of a String it means you can pass it Spanned (i.e. formatted) text.

If you don't want to want to build it by hand, and are more comfortable with HTML you can do this:

Toast.makeText(context, Html.fromHtml("<big>Big text.</big>"), Toast.LENGTH_SHORT)

Or, if using a string resource, do this:

<string name="big_text"><big>Big text.</big></string>

And just use it like this:

Toast.makeText(context, R.string.big_text, Toast.LENGTH_SHORT)

The available markup is mostly documented here.

Nuno Cruces
  • 1,584
  • 15
  • 18
0
    Toast toast = Toast.makeText(MyApplication.getContext(), "here", Toast.LENGTH_LONG);
    ViewGroup group = (ViewGroup) toast.getView();
    TextView messageTextView = (TextView) group.getChildAt(0);
    messageTextView.setTextSize(30);
    toast.show();
Mike Yang
  • 2,581
  • 3
  • 24
  • 27
0

Try this:

SpreadsheetApp.getActive().toast(" ","The text you want here");

By keeping the first set of quotes empty, the second set will bold your text and make it easier to read.

Normally the line would be: .toast("Text you want here","The title of your toast popup");

By making your text as the Title, second set of quotes, and leaving the first quotes empty, your text is now easier to read.

Burt
  • 3
  • 2
-2

You can try to put the following code into your Manifest:

<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"/> 

Put it above the <Application> element.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
reconditesea
  • 146
  • 5
  • How is this even related to what the actual question is? – Anuj Jan 11 '17 at 07:37
  • I'm guessing that this is an application-wide solution. Android might show larger toasts if it recognizes that the device has a large screen. – Paul Wintz May 23 '17 at 20:33