0

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); 

}
itaied
  • 6,827
  • 13
  • 51
  • 86

1 Answers1

0
DisplayMetrics dm = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(dm); int width=dm.widthPixels; int height=dm.heightPixels; int dens=dm.densityDpi; double wi=(double)width/(double)dens; double hi=(double)height/(double)dens; double x = Math.pow(wi,2); double y = Math.pow(hi,2); double screenInches = Math.sqrt(x+y);

as explained here

Try this..and let me know if it doesnt work..Will tell you some other options too...

Community
  • 1
  • 1
Lal
  • 14,726
  • 4
  • 45
  • 70
  • It's the same as I wrote in the onCreate method. It does return in pixels, but it includes the toolbar at the bottom of the screen. – itaied May 04 '14 at 18:30
  • Did you ment Context with upper case letter? Anyway, it says that getWindowManager is not a method of it... – itaied May 04 '14 at 18:38