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.
If You need to change the color of the view created then you need to use getView(). getBackground(). setColorFilter() function. If you need to add some icon along with the message in the toast view then you need to first get an instance of the TextView in the toast using getView().
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With