Is there any way that I can position my Toast message in the center of the screen? Since Toast.setGravity()
is not supported for Android 11 and above devices(API level 30 and above) as per android official documentation. I am not able to position my toast in the center of the screen. I searched all over for the solution but had no luck. My requirement is, I want to display a message similar to toast to be displayed at the center of the screen. I used a snack bar, but that is not what I am expecting. It is not necessary for me to display toast. I just want something to mimic the same functionality as that of a toast i.e. display a feedback message at the center of the screen.
Thank you.

- 3,248
- 6
- 30
- 50

- 21
- 1
- 5
-
You can use a Dialog. It's default position is in the center regardless the api – Khaby Lame Oct 28 '21 at 10:53
-
But how can we make it behave as a toast? – Allan Antony Oct 28 '21 at 10:58
-
What are you trying to say? – Khaby Lame Oct 28 '21 at 10:59
-
You want to say that the dialog should dissappear after 2 secs. Am I right? – Khaby Lame Oct 28 '21 at 10:59
-
Are you suggesting to use dialog for displaying the message and dismiss it after few seconds via postdelay? is that what you mean? – Allan Antony Oct 28 '21 at 11:00
-
Yes. I mean that – Khaby Lame Oct 28 '21 at 11:01
-
Yeah. that should work i guess .Thank you – Allan Antony Oct 28 '21 at 11:03
-
You can try it first. If it doesnt work i will find another alternative – Khaby Lame Oct 28 '21 at 11:04
2 Answers
The restriction on setting gravity for Toasts applies only to text toasts (created using the Toast.makeText(...)
method). You can create a regular toast (by calling standard constructor - Toast(activity)
), set gravity and customize it by setting a custom view.
As a result, you get something like this:
val customToast = Toast(this).also {
// View and duration has to be set
val view = LayoutInflater.from(context).inflate(R.layout.foo_custom_toast, null)
it.setView(view)
it.duration = Toast.LENGTH_LONG
it.setGravity(Gravity.CENTER, 0, 0)
}
Also, you can check this question and this article, I think this will help you.
P.s If you only need to show the text in your toast then in the Toast's custom view add a textView and then, when setting up the toast (setting the custom view) set the text to this textView. Also, through customView, you can customize Toast as you like.

- 321
- 5
- 15
Here's how to do it with a Snackbar, provided you have a screen-sized ConstraintLayout in your hierarchy.
In your main Activity's ConstraintLayout, add a horizontal guideline at the center of the screen:
<androidx.constraintlayout.widget.Guideline
android:id="@+id/centerHorizontalGuideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent=".50" />
Then in your Activity, you can set the anchor view of the Snackbar to this Guideline. Setting an anchor view tells it where to appear on the screen, instead of the default of the bottom.
fun showCenteredSnackbar(@StringRes messageId: Int, duration: Int = Snackbar.LENGTH_SHORT) {
Snackbar.make(this, binding.container, getText(messageId), duration)
.setAnchorView(binding.centerHorizontalGuideline)
.show()
}
where binding.container
is the ConstraintLayout.
Your fragments can call this function using (requireActivity() as MainActivity).showCenteredSnackbar()
.

- 83,111
- 11
- 94
- 154
-
Thanks for the info. I have already implemented the same using dialog but even snackbar is a good solution. – Allan Antony Oct 31 '21 at 05:25