How to Change Position of Toast in Android

How to change position of Toast in Android?

From the documentation,

Positioning your Toast

A standard toast notification appears near the bottom of the screen,
centered horizontally. You can change this position with the
setGravity(int, int, int) method. This accepts three parameters: a
Gravity constant, an x-position offset, and a y-position offset.

For example, if you decide that the toast should appear in the
top-left corner, you can set the gravity like this:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

If you want to nudge the position to the right, increase the value of
the second parameter. To nudge it down, increase the value of the last
parameter.

Change the Toast display position

Unfortunately setting Toast gravity is no longer supported. If you take a look at the documentation it specifically says:

Warning: Starting from Android Build.VERSION_CODES#R, for apps targeting API level Build.VERSION_CODES#R or higher, this method is a no-op when called on text toasts.


To get the required behaviour you would have to implement a custom solution.

Android - Customizing Position of Toast Message

From the docs:

You can change this position with the setGravity(int, int, int)
method. This accepts three parameters: a Gravity constant, an
x-position offset, and a y-position offset.

For example, if you decide that the toast should appear in the
top-left corner, you can set the gravity like this:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0); 

If you want to nudge
the position to the right, increase the value of the second parameter.
To nudge it down, increase the value of the last parameter.

So in your case, you could do:

//create toast object
Toast myToast = Toast.makeText(getApplicationContext(), greetings[rndy.nextInt(6)], Toast.LENGTH_SHORT);
//set gravity
myToast.setGravity(Gravity.CENTER_HORIZONTAL); //<-- set gravity here
//and show it
myToast.show();

How to Create a Toast at top of Screen In Android API 30+

If you're using the Material Components library, there's a setAnchorView method on its Snackbar implementation that basically lets you specify a view that the snackbar should be above. So if there's a suitable one in your layout (or you can add one, say a Guideline in the appropriate spot) you could use that.

More info here:
https://stackoverflow.com/a/58665768/13598222

I don't think Toasts are an option anymore, seems like that's a system feature now, and you don't get any control over how it renders them - you give it text, and that's it. No gravity, no custom views etc, no putting your own spin on it!

Creating a toast in android and specifying the location

From https://developer.android.com/reference/android/widget/Toast#Toast(android.content.Context) :

Construct an empty Toast object. You must call setView(View) before you can call show().

Your code crashes because now you are using the Toast() constructor. And you need to setView(View) before show(). While this is not necessary with makeText().

As I understand, makeText() creates a View for you setting the text from the second parameter. But the constructor doesn't, and the setText() method does not either. setText() only update the an existing text:

From https://developer.android.com/reference/android/widget/Toast#setText(int) :

Update the text in a Toast that was previously created using one of the makeText() methods.

Here is an example of how to do that:

Custom toast on Android: a simple example

Edit

In any case, you can create the Toast with your initial code, without calling show().

public void onClick(View V) {

Toast t = Toast.makeText(QuizActivity.this,
R.string.correct_toast,
Toast.LENGTH_SHORT);


t.setGravity(Gravity.TOP|Gravity.LEFT,0,0);
t.show();

}

Edit 2

Now you have edited your code, but you are passing the Button View.

How to display a Toast message in the top of the page in my android application

You need add this as programmatically like this in your code:

toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

It is possible to specify the position of a Toast?

Here is the documentation:

http://developer.android.com/guide/topics/ui/notifiers/toasts.html#Positioning



Related Topics



Leave a reply



Submit