23

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.

Ye Lin Aung
  • 11,234
  • 8
  • 45
  • 51

6 Answers6

35

The answer is here: https://stackoverflow.com/a/13231981

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);
    toastTV.setTextSize(30);
    toast.show();

This worked for me like a charm!

Community
  • 1
  • 1
DritanX
  • 753
  • 7
  • 18
20

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.

CaptJak
  • 3,592
  • 1
  • 29
  • 50
RoflcoptrException
  • 51,941
  • 35
  • 152
  • 200
11

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

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
}
hamidfzm
  • 4,595
  • 8
  • 48
  • 80
  • I think it is most correct way to set Toast typeface and save system Toast styling. – Nik Jul 30 '14 at 11:05
3

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

MontyThreeCard
  • 821
  • 9
  • 14
1

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()
naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
0

I used this solution in kotlin

in CustomView or Fragment

 fun persianToast(message: String): Toast {
    return Toast.makeText(context, message, Toast.LENGTH_SHORT).also {
        val view = it.view as LinearLayout
        val tv = view.getChildAt(0) as TextView
        val typeFace = Typeface.createFromAsset(context?.assets, MyApplication.getFont(MyApplication.LIGHT_FONT))
        tv.typeface = typeFace
    }
 }

MyApplication class :

class MyApplication : Application() {
companion object {
     const val NORMAL_FONT = 0
     const val BOLD_FONT = 1
     const val MEDIUM_FONT = 2
     const val LIGHT_FONT = 3
     const val ULTRA_LIGHT_FONT = 4

    @JvmStatic
    fun getFont(type: Int): String {
        return when (type) {
            LIGHT_FONT -> "font/fontLight.ttf"
            BOLD_FONT -> "font/fontBold.ttf"
            MEDIUM_FONT -> "font/fontMedium.ttf"
            ULTRA_LIGHT_FONT -> "font/fontUltraLight.ttf"
            else -> "font/fontNormal.ttf"
        }
    }
}

used in fragment:

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        Toast(context)
        persianToast("javid sattar").show()
}

good luck!!

Javid Sattar
  • 749
  • 8
  • 14