android – Change Toast Font-ThrowExceptions

Exception or error:

Currently, I’m trying to develop an app.
and I don’t know how to change the Toast font. .

 final OnClickListener clickListener = new OnClickListener() {

    public void onClick(View v) {
            try {
                Toast.makeText(nova.this,"Hello", 500000).show();
            catch (Exception e) {
                Toast.makeText(nova.this,"Exception:" +e, 500000);

I want to change the text “Hello” with custom font I’ve tried with TypeFace.

and Then, I want to set a variable at the place “TextClicked” .. I’ve tried with a local variable .. but it doesn’t work

any help with example source code will be really great for me.

How to solve:

From the official documentation:

Create your custom ToastView

If a simple text message isn’t enough, you can create a customized layout for your toast notification. To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView(View) method.

Following the link to the official Google Documentation will provide examples.


The answer is here:

After refactoring a little:

    Toast toast = Toast.makeText(context, R.string.yummyToast, Toast.LENGTH_SHORT);
    LinearLayout toastLayout = (LinearLayout) toast.getView();
    TextView toastTV = (TextView) toastLayout.getChildAt(0);

This worked for me like a charm!


You can use a SpannableString to set the font:

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/ATaha.ttf");
SpannableString efr = new SpannableString("Toast font changed!");
efr.setSpan(new TypefaceSpan(font), 0, efr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Toast.makeText(this, efr, Toast.LENGTH_SHORT).show();

A custom Span class that has a specific Typeface set:

public class TypefaceSpan extends MetricAffectingSpan {
    private Typeface mTypeface;
    public TypefaceSpan(Typeface typeface) {
        mTypeface = typeface;

    public void updateMeasureState(TextPaint p) {
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);

    public void updateDrawState(TextPaint tp) {
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);


Unfortunately the code on the Java page is bugged. Here is a link to a working function you can implement that gives you the text (I know, because I tested it), and with a little ingenuity, could be expanded to pass arguments for size, color, etc…

Toast Font size function here


Kotlin function:

fun makeLargeTextToast(text: CharSequence): Toast {
    return Toast.makeText(applicationContext, text, Toast.LENGTH_LONG).also {
        val toastLayout = it.view as LinearLayout
        val toastTV = toastLayout.getChildAt(0) as TextView
        toastTV.textSize = 30f

Use it as:

makeLargeTextToast("text message").show()

Leave a Reply

Your email address will not be published. Required fields are marked *