0

I have used Gravity.FILL and Gravity.FILL_HORIZONTAL to make a toast full screen. But it's not working. It's only filling half of the screen horizontaly.

Toast.java

Toast myToast = Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT);
myToast.setMargin(0,0);
myToast.setGravity( Gravity.FILL, 0, 0);
//myToast.setGravity( Gravity.FILL_VERTICAL | Gravity.FILL_HORIZONTAL, 0, 0);

myToast.show();

The result

Image_Link

TofferJ
  • 4,216
  • 1
  • 31
  • 43
miniature
  • 42
  • 6

1 Answers1

0

What actually happens here is that the width of the toast is set to wrap the content. If you add a longer string you'll see that it grows to cover the entire screen.

A somewhat hacky way to force it to always fill the entire screen is to simply change the width of the text field within the Toast like this:

Toast myToast = Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT);
myToast.setGravity(Gravity.FILL_VERTICAL, 0, 0);

TextView toastMessage = myToast.getView().findViewById(android.R.id.message);
// In case the name of the TextView is different for different versions of Wear OS.
if (toastMessage != null) {  
    toastMessage.setMinWidth(480); // Replace with the actual screen width
}

myToast.show();

It's not pretty. It's not future-proof. But it works. :)

An alternative way of solving it would be to provide your own view for the Toast. This would eliminate the risk of the code unexpectedly breaking in the future. The downside is that your toast might look different from other generic Toasts (and this might be confusing to the user).

My recommendation is to simply use the Toast as it is. Don't try to force it to fill the screen. Let it show in its default place with its default size. This ensures a both safe and consistent experience.

TofferJ
  • 4,216
  • 1
  • 31
  • 43