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
Creating a custom-styled Toast
in Android using Kotlin involves defining a custom XML layout for the toast and then inflating and displaying that layout. Here's a step-by-step guide:
First, create a new XML layout file in your res/layout
directory. Let's name it custom_toast_layout.xml
:
<?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:background="#444" android:padding="16dp" android:orientation="horizontal" android:gravity="center_vertical"> <ImageView android:id="@+id/toast_icon" android:layout_width="24dp" android:layout_height="24dp" android:src="@drawable/ic_launcher_foreground" /> <TextView android:id="@+id/toast_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingStart="16dp" android:text="Your Toast Message" android:textColor="#FFF" /> </LinearLayout>
Adjust the design as per your needs.
In your Kotlin Activity
or Fragment
, you can create a function to inflate and show the custom Toast
:
fun showCustomToast(message: String) { val layoutInflater = layoutInflater val customToastRoot = layoutInflater.inflate(R.layout.custom_toast_layout, null) val customToastText: TextView = customToastRoot.findViewById(R.id.toast_text) customToastText.text = message with(Toast(applicationContext)) { setGravity(Gravity.CENTER, 0, 0) duration = Toast.LENGTH_LONG view = customToastRoot show() } }
To display the custom-styled toast, simply call the showCustomToast()
function:
showCustomToast("This is a custom toast!")
Always keep in mind that, while creating a custom Toast
gives you flexibility and can enhance the UX, it's essential not to stray too far from standard Android designs. Users should easily recognize and understand the feedback you're providing.
Styling Toast messages in Android using Kotlin example code:
val toast = Toast.makeText(context, "Styled Toast", Toast.LENGTH_SHORT) val view = toast.view view.setBackgroundColor(ContextCompat.getColor(context, R.color.toastBackground)) val text = view.findViewById<TextView>(android.R.id.message) text.setTextColor(ContextCompat.getColor(context, R.color.toastText)) text.textSize = 18f toast.show()
Creating a custom layout for Toast in Android with Kotlin:
val inflater = layoutInflater val layout: View = inflater.inflate(R.layout.custom_toast_layout, findViewById(R.id.custom_toast_container)) val toast = Toast(context) toast.duration = Toast.LENGTH_SHORT toast.view = layout toast.show()
Customizing duration and position of Toast messages in Kotlin:
val toast = Toast.makeText(context, "Custom Duration and Position", Toast.LENGTH_LONG) toast.setGravity(Gravity.TOP or Gravity.START, 0, 0) toast.show()
Handling custom animations for Toast in Android using Kotlin:
val toast = Toast.makeText(context, "Custom Animation", Toast.LENGTH_SHORT) toast.view.setBackgroundResource(R.drawable.custom_toast_background) val animation = AnimationUtils.loadAnimation(context, R.anim.custom_toast_animation) toast.view.startAnimation(animation) toast.show()
Using custom Typeface for Toast text in Android with Kotlin:
Typeface
(font) to the text of a Toast message.val toast = Toast.makeText(context, "Custom Typeface", Toast.LENGTH_SHORT) val typeface = Typeface.createFromAsset(assets, "custom_font.ttf") val text = toast.view.findViewById<TextView>(android.R.id.message) text.typeface = typeface toast.show()
Creating a rounded corner Toast in Android using Kotlin:
val toast = Toast.makeText(context, "Rounded Corner Toast", Toast.LENGTH_SHORT) val view = toast.view val background = ContextCompat.getDrawable(context, R.drawable.rounded_corner_background) view.background = background toast.show()