Android Development
Toast Positioning
UI Customization
Android Tutorials
Mobile App Development

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.

Browse interview questions

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.

java
1import android.os.Bundle;
2import android.view.Gravity;
3import android.widget.Toast;
4import androidx.appcompat.app.AppCompatActivity;
5
6public class MainActivity extends AppCompatActivity {
7    @Override
8    protected void onCreate(Bundle savedInstanceState) {
9        super.onCreate(savedInstanceState);
10
11        Toast toast = Toast.makeText(this, "Saved", Toast.LENGTH_SHORT);
12        toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 100);
13        toast.show();
14    }
15}

In this example:

  • 'Gravity.TOP | Gravity.CENTER_HORIZONTAL places the toast near the top center'
  • 'xOffset is 0'
  • 'yOffset is 100 pixels 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:

kotlin
val toast = Toast.makeText(this, "Centered message", Toast.LENGTH_SHORT)
toast.setGravity(Gravity.CENTER, 0, 0)
toast.show()

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.

kotlin
1import com.google.android.material.snackbar.Snackbar
2
3Snackbar.make(findViewById(android.R.id.content), "Saved", Snackbar.LENGTH_SHORT)
4    .show()

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:

kotlin
1val yOffsetPx = (64 * resources.displayMetrics.density).toInt()
2val toast = Toast.makeText(this, "Saved", Toast.LENGTH_SHORT)
3toast.setGravity(Gravity.TOP or Gravity.CENTER_HORIZONTAL, 0, yOffsetPx)
4toast.show()

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 setGravity for text toasts the same way.
  • Building important UI flows around exact toast placement.
  • Using raw pixel offsets without accounting for screen density.
  • Reaching for Toast when Snackbar is 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 x and y offsets 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, Snackbar is often the better modern solution.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.