info@androidpaper.co.in

How to Change Toast font?

In Android, the Toast class does not provide a direct way to change the font. However, you can achieve the desired effect by creating a custom Toast layout and applying a custom Typeface (font) to the TextView within that layout. Here's how you can change the font of a Toast:

Create a custom layout XML file:
Create a new XML file (e.g., custom_toast_layout.xml) in your project's res/layout directory. Define the layout structure for the custom Toast, including a TextView element for displaying the Toast message. You can customize the layout as per your requirements. For example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/toast_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="#FFFFFF"
        android:fontFamily="@font/your_custom_font" />
</LinearLayout>

In this example, we have a LinearLayout as the root element, containing a TextView with an id "toast_text". The TextView specifies a custom font using the android:fontFamily attribute. Replace @font/your_custom_font with the desired font resource, or use the android:typeface attribute to set a system font style.

Create a custom Toast:
In your Java/Kotlin code, create a custom Toast by inflating the custom layout XML file and applying it to a Toast object. You can do this as follows:
Java:

// Inflate the custom layout
View layout = getLayoutInflater().inflate(R.layout.custom_toast_layout, findViewById(R.id.custom_toast_container));

// Find the TextView in the custom layout
TextView toastText = layout.findViewById(R.id.toast_text);
toastText.setText("Your Toast Message");

// Create and display the Toast
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();

Kotlin:

/ Inflate the custom layout
val layout = layoutInflater.inflate(R.layout.custom_toast_layout, findViewById(R.id.custom_toast_container))

// Find the TextView in the custom layout
val toastText: TextView = layout.findViewById(R.id.toast_text)
toastText.text = "Your Toast Message"

// Create and display the Toast
val toast = Toast(applicationContext)
toast.duration = Toast.LENGTH_SHORT
toast.view = layout
toast.show()

In this code, we inflate the custom layout using getLayoutInflater().inflate(). Then, we find the TextView within the custom layout using findViewById() and set the desired text for the Toast. Finally, we create a new Toast object, set its duration, and apply the custom layout using setView().
Run your app to see the custom Toast with the changed font.
By following these steps, you can create a custom Toast layout and change the font of the Toast message by applying a custom Typeface to the TextView within the layout.

To change the Toast font with a button click, you can dynamically update the font of the Toast message by setting a custom Typeface when the button is clicked. Here's an example of how you can achieve this:
Create a custom layout XML file:
Follow the steps mentioned earlier to create a custom layout XML file (e.g., custom_toast_layout.xml) that defines the layout structure for the custom Toast. Make sure to include a TextView element to display the Toast message with the desired font. Set up the button click listener:
In your activity or fragment, find the button view and set a click listener to handle the button click event. Inside the click listener, update the font of the Toast message. Here's an example in Java:

Button changeFontButton = findViewById(R.id.change_font_button);
changeFontButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Create a new Typeface with the desired font
        Typeface customTypeface = Typeface.createFromAsset(getAssets(), "fonts/your_custom_font.ttf");

        // Get the default Toast view
        Toast toast = Toast.makeText(getApplicationContext(), "Your Toast Message", Toast.LENGTH_SHORT);

        // Get the TextView from the default Toast view
        TextView toastTextView = toast.getView().findViewById(android.R.id.message);

        // Set the custom Typeface for the TextView
        toastTextView.setTypeface(customTypeface);

        // Show the updated Toast message
        toast.show();
    }
});

Another Written Description

And here's the equivalent code in Kotlin:

val changeFontButton: Button = findViewById(R.id.change_font_button)
changeFontButton.setOnClickListener {
    // Create a new Typeface with the desired font
    val customTypeface = Typeface.createFromAsset(assets, "fonts/your_custom_font.ttf")

    // Get the default Toast view
    val toast = Toast.makeText(applicationContext, "Your Toast Message", Toast.LENGTH_SHORT)

    // Get the TextView from the default Toast view
    val toastTextView = toast.view.findViewById<TextView>(android.R.id.message)

    // Set the custom Typeface for the TextView
    toastTextView.typeface = customTypeface

    // Show the updated Toast message
    toast.show()
}

In this code, we first create a new Typeface object by loading the desired font from the assets folder using Typeface.createFromAsset(). Then, when the button is clicked, we retrieve the default Toast view using Toast.makeText() and modify the TextView within the Toast view using toast.getView().findViewById(). We set the custom Typeface to the TextView using setTypeface(). Finally, we show the updated Toast message using toast.show().

Run your app, click the button, and observe the changed font of the Toast message when the button is clicked. By following these steps, you can change the Toast font dynamically by updating the Typeface of the Toast message when a button is clicked.