Android SharedPreferences Tutorial and Example

Styling Custom Toast Messages in Android Apps

A toast is the smallest unit of feedback an Android app can offer: a short message that appears over the current screen and then gets out of the way. The stock version, created with Toast.makeText, is quick to reach for and even quicker to ship, but it also looks identical on every device, in every app, and for every type of message.

Most beginner projects never outgrow that default. As soon as an app needs branded colours, a supporting icon, a heading and body copy, or a message that sits in a particular corner of the screen, the plain text toast starts to feel like a placeholder rather than a designed component. Building a styled toast takes one custom layout, one shape drawable and a handful of view properties, and it gives you a notification you can actually match to your app's visual language.

Here is how the common options line up before you write any code:

Capability Default toast Custom toast Snackbar
Layout Text only XML layout with icon, heading and body Material component with action
Branding System fonts and colours Your own palette, shape and typography Theme aware
Placement Fixed near the screen edge Any gravity and offset you set Anchored to the parent view
Interaction Tap to dismiss Optional button or custom view Action plus swipe to dismiss
Duration Short or long Short or long Configurable length

Why the stock toast looks like every other app

The default toast is drawn by the system, which means it inherits the platform's typeface, background shape and padding rather than your app's resources. It also floats near the bottom of the screen in the same position every time, so it can never carry the weight of a heading, a product thumbnail or a confirmation icon.

That matters in markets where apps are judged on polish. Australian developers working on products for ANZ, CBA, Atlassian or one of the big retail platforms will often have a design system already defined in colours and type styles, and a system-rendered toast simply ignores all of it. A team that ships a Melbourne-based retail app will usually have brand tokens ready to go, so reusing them in a notification is the path of least resistance.

A snackbar remains a strong alternative when you need an action button, a swipe gesture or an inline message tied to a particular view. A custom toast, though, is the better fit for transient confirmations that float above the whole activity: "Saved to favourites", "Photo uploaded", "Order placed". It stays lightweight, it works from a background service or a broadcast receiver, and it never steals layout space from your content.

Constructing a custom toast layout

Start by creating a layout resource, for example res/layout/toast_message.xml. A vertical LinearLayout with a small ImageView and a nested text block gives you room for an icon plus a bold title and a single line of supporting text.

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:id="@+id/toastTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/toastBody"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="13sp" />
</LinearLayout>

Inflate that layout with LayoutInflater, assign the strings, and hand the resulting view to the toast. Because the view is yours, you control every attribute inside it.

val view = layoutInflater.inflate(R.layout.toast_message, null)
view.findViewById<TextView>(R.id.toastTitle).text = "Order placed"
view.findViewById<TextView>(R.id.toastBody).text = "We'll text you when it ships"

Toast.makeText(context, view, Toast.LENGTH_SHORT).show()

Passing a view instead of a string is the whole trick. From this point on, the toast behaves like any other view you build, so constraints, dividers, rounded corners and custom fonts all become available.

Giving the toast your own colours and shape

A rounded background is what makes a custom toast look designed rather than default. Create res/drawable/toast_background.xml as a shape drawable with a solid colour, a corner radius and a small amount of padding baked in. Referencing colour resources directly rather than hard-coded hex values keeps the toast consistent with the rest of your palette and lets you ship a dark mode variant.

Typography is just as important. Pull the title text appearance from your styles file so it matches buttons and headers, and pick a body colour that clears contrast requirements on the chosen background. Accessibility reviewers will check exactly this: a pale grey message on a white card is invisible to anyone with low vision, and screen readers need the title and body exposed as separate, meaningful labels.

For copy, keep it short and conversational. A local example for a Sydney ferry booking app might read "Ferry booked — no worries, we'll send the ticket to your email", which is friendly without becoming verbose. Tossing in a bit of everyday Australian phrasing ("Arvo specials saved", "All good, you're sorted") works when it suits the brand, but avoid slang that would confuse international users of the same app.

Controlling placement and how long it stays

setGravity on the toast object decides which corner it anchors to, and Gravity.TOP or Gravity.BOTTOM combined with Gravity.END will pin it to the upper or lower right of the screen. setView also accepts measured width and height values, so a toast can be stretched to match a specific design mock-up if you need an edge-to-edge banner.

Duration still comes down to LENGTH_SHORT and LENGTH_LONG, and there is no third option. If a message needs to stay readable for longer, the better solution is to write less rather than to invent a new timing rule. Custom toasts are also slightly limited on newer releases: since Android 12, the system renders custom toast views itself and sanitises some of the styling, so always test your layout on the oldest and newest API levels you support.

For repeated messages, hold a reference to the toast and call show again instead of creating a new instance each time, which prevents a stack of identical notifications from piling up.

Testing on real devices and refining the details

Emulators are useful for checking layout maths, but a real handset reveals what the toast actually looks like over a busy screen. Australian teams often test across a spread of Samsung, Google and lower-end Motorola devices, since Android's market share here sits comfortably ahead of iOS and cheaper models are common. Open the app on a real phone, trigger the toast over a photo-heavy screen, and check that the contrast still holds.

Add a small delay before showing the message when it follows a user action, so the toast does not appear while the eye is still on the button that was tapped. Log the toast triggers during development, since a message that fires on the wrong thread or after the activity has finished is a common source of bug reports.

Developers who prefer a keyboard-driven workflow often spend time tuning their editor before touching Android code, and your first Magik extension is a reasonable way to get comfortable with that tooling. Once the layout, drawable and copy feel right, keep the toast in a small reusable helper so every screen in the app shares the same corner radius, padding and duration.