-3

what is x-offset and y-offset in setGravity? what is the maximum value for it?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

2

setGravity(int, int, int) method 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 below: toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

Note: measure unit is pixel

X-position: In 2 dimensional, its X coordinate of the Toast (min value:0)

Y-position: In 2 dimensional, its Y coordinate of the Toast (min value:0)

For more Details, Check this Link

Ankur Mishra
  • 103
  • 9
  • thanks man, but can you please tell me what is the measure unit of x-position and y-position? I mean is it dp or inch or pixel? – Kabir Malik Oct 25 '17 at 09:54
  • Updated my answer. – Ankur Mishra Oct 25 '17 at 09:58
  • Can you please provide a source, where it is mentioned, that `offsetX` and `offsetY` cannot take negative values? – azizbekian Oct 25 '17 at 10:28
  • It's just (0,0) is the top position and since measure unit is Pixel and not relative position, its bad practice to use negative values in it. Adding negative value might change the margins of toast and if that is required then it can be achieved by other ways also. So yeah, we can use negative value(since type is int) but its bad practice. – Ankur Mishra Oct 25 '17 at 10:52
  • @KabirMalik If your issue is solved, mark as settled. – Ankur Mishra Oct 25 '17 at 18:44