What is the range of the x,y Offset of the Toast.setGravity?

898 Views Asked by At

I am testing a code where i want some toast text to appear randomly allover the phone screen. What is the max value the x and y offset of a Toast Gravity?

int i=0;
while(i < 100){
    Random rand = new Random();
    int xOffset = rand.nextInt(500); //what is the max number for this?
    int yOffset = rand.nextInt(700); //what is the max number for this?   
    Context context = getApplicationContext();
    CharSequence text = "Hello toast!";
    int duration = Toast.LENGTH_SHORT;
    Toast toast = Toast.makeText(context, text, duration);
    toast.setGravity(Gravity.TOP|Gravity.LEFT,xOffset,yOffset);
    toast.show();
    i++;
}
1

There are 1 best solutions below

0
theBrainyGeek On BEST ANSWER

The official doc does not specifically tells us this. I assume, Gravity.CENTER_VERTICAL will put the toast in the middle of the screen. x and y offset are in pixels, so the maximum is your display width/height.