How to change position of Toast in Android?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Classic Android Toast messages can be repositioned with setGravity, but the answer needs one caveat: modern Android behavior is more restricted than old tutorials suggest. For older text toasts, changing the position is straightforward. For newer platform behavior, especially when targeting Android 11 and above, toast customization is less reliable and often not the best UI choice anyway.
The Traditional Way: setGravity
The API most developers mean is Toast.setGravity(...). It lets you choose a gravity and offsets before showing the toast.
In this example:
- '
Gravity.TOP | Gravity.CENTER_HORIZONTALplaces the toast near the top center' - '
xOffsetis0' - '
yOffsetis100pixels down from the top anchor'
That is the standard answer for classic Android code.
Common Gravity Patterns
A few combinations show up repeatedly:
- '
Gravity.TOP | Gravity.CENTER_HORIZONTAL' - '
Gravity.CENTER' - '
Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL'
For example, centered:
Offsets let you fine-tune the result relative to the chosen anchor.
Modern Android Caveat
This is the part many old answers miss. Android’s official Toast behavior changed over time, and on newer platform versions the system has tighter control over how text toasts are displayed. For apps targeting newer Android versions, position changes for standard text toasts may not behave the same way older devices did.
That means code using setGravity can be:
- fully effective on older devices
- partially ignored on newer devices
- inconsistent across device makers and OS versions
So the correct modern guidance is not just "call setGravity." It is "call setGravity if you are working with classic toast behavior, but do not build important UX around precise toast placement."
When to Use Snackbar Instead
If you need a message positioned predictably relative to the app UI, Snackbar is often a better choice. It is designed for app-level feedback and behaves more consistently in modern Material-style interfaces.
A Snackbar is usually the better tool when:
- the message is tied to a specific screen action
- consistent placement matters
- you want an optional action button
In other words, if the real requirement is a controllable in-app notification, the answer may be "do not use Toast for that."
Avoid Pixel Assumptions
If you do use setGravity, remember that the offsets are in pixels, not density-independent units. Hard-coded pixel offsets can look different across devices with different screen densities.
If you need a more controlled offset, convert from dp first:
That gives you more consistent placement on different screens, even though the system may still impose limits on newer Android versions.
Common Pitfalls
- Assuming all Android versions honor
setGravityfor text toasts the same way. - Building important UI flows around exact toast placement.
- Using raw pixel offsets without accounting for screen density.
- Reaching for
ToastwhenSnackbaris the better app-level feedback component. - Expecting toast customization to be reliable across all modern devices and target SDK levels.
Summary
- The classic way to change toast position is
toast.setGravity(...). - Gravity and
xandyoffsets control the traditional toast position. - Modern Android behavior is more restrictive, so exact placement is not always dependable.
- Convert offsets carefully and avoid relying on pixel assumptions.
- If predictable in-app feedback matters,
Snackbaris often the better modern solution.
Related reading
- How to change progress bar's progress color in Android
- How to change ProgressBar's progress indicator color in Android
- How to change spinner text size and text color?
- How to change Status Bar text color in iOS
- How to change Status Bar text color in iOS
- How to change Status Bar text color in iOS
- How to change styling of TextInput placeholder in React Native?
- How to change the background color of a UIButton while it's highlighted?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.