How to change position of Toast in Android?
AndroidToastAndroid Problem Overview
When I use Toast
to display some popup text on the screen, it displays the text a little bit above the bottom of the screen, which is the default position.
Now I want to display it in the middle of screen or somewhere according to my choice.
Can anyone guide me how to achieve this?
Android Solutions
Solution 1 - 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.
Solution 2 - Android
If you get an error indicating that you must call makeText, the following code will fix it:
Toast toast= Toast.makeText(getApplicationContext(),
"Your string here", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
Solution 3 - Android
You can customize the location of your Toast by using:
setGravity(int gravity, int xOffset, int yOffset)
This allows you to be very specific about where you want the location of your Toast to be.
One of the most useful things about the xOffset and yOffset parameters is that you can use them to place the Toast relative to a certain View.
For example, if you want to make a custom Toast that appears on top of a Button, you could create a function like this:
// v is the Button view that you want the Toast to appear above
// and messageId is the id of your string resource for the message
private void displayToastAboveButton(View v, int messageId)
{
int xOffset = 0;
int yOffset = 0;
Rect gvr = new Rect();
View parent = (View) v.getParent();
int parentHeight = parent.getHeight();
if (v.getGlobalVisibleRect(gvr))
{
View root = v.getRootView();
int halfWidth = root.getRight() / 2;
int halfHeight = root.getBottom() / 2;
int parentCenterX = ((gvr.right - gvr.left) / 2) + gvr.left;
int parentCenterY = ((gvr.bottom - gvr.top) / 2) + gvr.top;
if (parentCenterY <= halfHeight)
{
yOffset = -(halfHeight - parentCenterY) - parentHeight;
}
else
{
yOffset = (parentCenterY - halfHeight) - parentHeight;
}
if (parentCenterX < halfWidth)
{
xOffset = -(halfWidth - parentCenterX);
}
if (parentCenterX >= halfWidth)
{
xOffset = parentCenterX - halfWidth;
}
}
Toast toast = Toast.makeText(getActivity(), messageId, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, xOffset, yOffset);
toast.show();
}
Solution 4 - Android
From the documentation:
Warning: Starting from Android Build.VERSION_CODES#R, for apps targeting API level Build.VERSION_CODES#R or higher, this method (setGravity) is a no-op when called on text toasts.
Which means that setGravity
can no longer be used in API 30+ and will have to find another to achieve the required behaviour.
Solution 5 - Android
Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
Solution 6 - Android
Toast mytoast= Toast.makeText(getApplicationContext(), "Toast Message", 1);
mytoast.setGravity(Gravity.CENTER_HORIZONTAL, 0, 0); // for center horizontal
//mytoast.setGravity(Gravity.CENTER_VERTICAL); // for center vertical
//mytoast.setGravity(Gravity.TOP); // for top
mytoast.show();
The above code is will help u to display toast in the middle of screen or according to ur choice for that just set the toast gravity according to ur need
Note: For this process u have to use object of Toast
Solution 7 - Android
The method to change the color, position and background color of toast is:
Toast toast=Toast.makeText(getApplicationContext(),"This is advanced toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT,0,0);
View view=toast.getView();
TextView view1=(TextView)view.findViewById(android.R.id.message);
view1.setTextColor(Color.YELLOW);
view.setBackgroundResource(R.color.colorPrimary);
toast.show();
For line by line explanation: https://www.youtube.com/watch?v=5bzhGd1HZOc
Solution 8 - Android
setting toast at topin screen
toast.setView(view);
toast.setGravity(Gravity.BOTTOM , 0, 0); // here i am setting toast at bottom
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
now at bottom
toast.setView(view);
toast.setGravity(Gravity.BOTTOM , 0, 0); // here i am setting toast at bottom
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
the same way we can set toast in left, right and also center
Click [here](http://androidcoding.in/2016/05/31/android-tutorial-custom-toast/ "android custom toast")
Solution 9 - Android
//A custom toast class where you can show custom or default toast as desired)
public class ToastMessage {
private Context context;
private static ToastMessage instance;
/**
* @param context
*/
private ToastMessage(Context context) {
this.context = context;
}
/**
* @param context
* @return
*/
public synchronized static ToastMessage getInstance(Context context) {
if (instance == null) {
instance = new ToastMessage(context);
}
return instance;
}
/**
* @param message
*/
public void showLongMessage(String message) {
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
}
/**
* @param message
*/
public void showSmallMessage(String message) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
/**
* The Toast displayed via this method will display it for short period of time
*
* @param message
*/
public void showLongCustomToast(String message) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View layout = inflater.inflate(R.layout.layout_custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.ll_toast));
TextView msgTv = (TextView) layout.findViewById(R.id.tv_msg);
msgTv.setText(message);
Toast toast = new Toast(context);
toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
/**
* The toast displayed by this class will display it for long period of time
*
* @param message
*/
public void showSmallCustomToast(String message) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
View layout = inflater.inflate(R.layout.layout_custom_toast, (ViewGroup) ((Activity) context).findViewById(R.id.ll_toast));
TextView msgTv = (TextView) layout.findViewById(R.id.tv_msg);
msgTv.setText(message);
Toast toast = new Toast(context);
toast.setGravity(Gravity.FILL_HORIZONTAL | Gravity.BOTTOM, 0, 0);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(layout);
toast.show();
}
}
Solution 10 - Android
For people like me, looking for an answer to set the gravity on Toasts on Android R and above, see this article.
Solution 11 - Android
Toast toast = Toast.makeText(test.this,"bbb", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
//this is the best solution to counter any error
Solution 12 - Android
SOLUTION
fun Context.showToastOnTOP(message: String) = Toast.makeText(this, message, Toast.LENGTH_SHORT)
.apply { setGravity(Gravity.TOP, 0, 0); show() }
fun Context.showToastOnBOTTOM(message: String) = Toast.makeText(this, message, Toast.LENGTH_SHORT)
.apply { setGravity(Gravity.BOTTOM, 0, 0); show() }
Important note Gravity
will only work if your app target max 29 or less.
Chill Pill :)
Solution 13 - Android
Toast toast = Toast.makeText(this, "Custom toast creation", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM | Gravity.RIGHT,0,0);
toast.show();