I'm writing an app in Java and I want to place a button randomly in the screen. I used the Display.getSize(), but it gives me the whole size of the screen including the bottom bar of the return, home and recent activities buttons. So my button which is randomly located in the screen, often is hidden under this bar. How can I take into cosideration this bar in my calculation?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hiding_buttons);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
SCREEN_WIDTH = size.x;
SCREEN_HEIGHT = size.y;
}
public void button_hideButtonClick(View view) {
// init button location to beginning position
view.offsetLeftAndRight(-horizonOffset);
view.offsetTopAndBottom(-verticalOffset);
Random r = new Random();
// generate random pos relative to the screen
horizonOffset = r.nextInt(SCREEN_WIDTH-view.getWidth());
verticalOffset = r.nextInt(SCREEN_HEIGHT-view.getHeight());
// relocate the button randomly
view.offsetLeftAndRight(horizonOffset);
view.offsetTopAndBottom(verticalOffset);
}