Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Changing the font of a Toast
in Android involves customizing the layout of the Toast
. Here's a step-by-step guide:
Place your custom font (e.g., my_custom_font.ttf
) inside the res/font
directory of your project. If the font
directory doesn't exist, create it.
Create a new layout file named custom_toast.xml
in the res/layout
directory:
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:background="#CC000000" android:textColor="#FFFFFF" android:fontFamily="@font/my_custom_font"/>
In your activity or fragment:
import android.graphics.Typeface import android.os.Bundle import android.view.LayoutInflater import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // Call the function to show the toast showToastWithCustomFont("Hello with custom font!") } private fun showToastWithCustomFont(message: String) { val inflater = LayoutInflater.from(this) val layout = inflater.inflate(R.layout.custom_toast, findViewById(R.id.rootLayout)) val text: TextView = layout.findViewById(R.id.textToast) text.text = message // If you want to further change the typeface (e.g., bold): // text.typeface = Typeface.create(resources.getFont(R.font.my_custom_font), Typeface.BOLD) val toast = Toast(this) toast.duration = Toast.LENGTH_LONG toast.view = layout toast.show() } }
Replace R.id.rootLayout
with the ID of your root layout if you have another name for it.
In the showToastWithCustomFont
function:
TextView
.Now, when you call the showToastWithCustomFont
function, it'll display the message using the custom font you set in the custom_toast.xml
layout.
Customize Toast message font in Android:
Customize the font of a Toast message by creating a custom TextView
and setting its font:
val toast = Toast.makeText(this, "Custom Font Toast", Toast.LENGTH_SHORT) val view = toast.view // Create a custom TextView val customTextView = TextView(this) customTextView.text = "Custom Font Toast" customTextView.typeface = customFont // Set your custom font // Set the custom TextView as the Toast view toast.view = customTextView toast.show()
Setting custom font for Toast in Android:
Define a custom Typeface
and apply it to the TextView
inside the Toast:
val customFont = Typeface.createFromAsset(assets, "custom_font.ttf") // Load your custom font val toast = Toast.makeText(this, "Custom Font Toast", Toast.LENGTH_SHORT) val view = toast.view // Get the TextView from the default Toast layout val textView = view.findViewById<TextView>(android.R.id.message) // Apply the custom font to the TextView textView.typeface = customFont toast.show()
Custom Toast with custom font example code:
Create a custom layout for the Toast and set a custom font:
val inflater = layoutInflater val layout: View = inflater.inflate(R.layout.custom_toast_layout, findViewById(R.id.custom_toast_root)) // Set your custom font to the TextView in the custom layout val customTextView = layout.findViewById<TextView>(R.id.customToastText) customTextView.typeface = customFont val toast = Toast(applicationContext) toast.duration = Toast.LENGTH_SHORT toast.view = layout toast.show()
Change Toast text appearance in Android:
Customize the appearance of the Toast text using a custom SpannableString
:
val toast = Toast.makeText(this, "Custom Text Appearance", Toast.LENGTH_SHORT) val spannableString = SpannableString("Custom Text Appearance") spannableString.setSpan(ForegroundColorSpan(Color.BLUE), 0, spannableString.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) val view = toast.view val textView = view.findViewById<TextView>(android.R.id.message) textView.text = spannableString toast.show()
Using custom Typeface for Toast message in Android:
Apply a custom Typeface
to the Toast message:
val toast = Toast.makeText(this, "Custom Typeface Toast", Toast.LENGTH_SHORT) val view = toast.view val textView = view.findViewById<TextView>(android.R.id.message) // Apply the custom Typeface to the TextView textView.typeface = customFont toast.show()
Modify Toast style and font in Android app:
Customize the style and font of the Toast by creating a custom layout and setting a custom Typeface
:
val inflater = layoutInflater val layout: View = inflater.inflate(R.layout.custom_toast_layout, findViewById(R.id.custom_toast_root)) val customTextView = layout.findViewById<TextView>(R.id.customToastText) customTextView.typeface = customFont val toast = Toast(applicationContext) toast.duration = Toast.LENGTH_SHORT toast.view = layout toast.show()
Toast.makeText with custom font in Android:
Use Toast.makeText
with a custom font:
val toast = Toast.makeText(this, "Custom Font Toast", Toast.LENGTH_SHORT) val view = toast.view // Get the TextView from the default Toast layout val textView = view.findViewById<TextView>(android.R.id.message) // Apply the custom font to the TextView textView.typeface = customFont toast.show()
Customizing Toast duration and position in Android:
Customize the Toast duration and position:
val toast = Toast.makeText(this, "Custom Duration and Position", Toast.LENGTH_LONG) toast.setGravity(Gravity.TOP or Gravity.START, 0, 0) // Set custom position val view = toast.view val textView = view.findViewById<TextView>(android.R.id.message) // Apply any other customizations as needed toast.show()