I'm trying to make an Android snake game, and I want to have a grid that stretches in a square the width of the screen (Portrait).
I an using a drawable storing my png
, and when i add the line grid.setBounds(0,0,screenWidth,screenWidth)
my phone displays a white screen at first, though it works as expected once I minimize and reopen my application.
The app also works as expected if I put standard numbers in as parameters. This is on my nexus 6P on android 7. When I emulate it on an older device it works, but emulated on a newer device, I see the same issue. I get the screen width as follows:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
scale = screenWidth/1024.0;
If anyone could help that would be much appreciated.
Try to get the screen size in onWindowFocusChnaged
method of activity.
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
scale = screenWidth/1024.0;
}