0

enter image description here

Hello. I draw a rectangle overlay on Android Canvas.

canvas.drawRect(0, 0, 1000, 1000, paint);

That is my code. I set start point is (0,0) that is (left, top) coordinate. But (0,0) is not on a left-top end point of full screen. No matter how width and height are big, canvas cannot cover full screen also.

How can I solve this problem?

2 Answers2

1

If you want a fullscreen view, use the setContentView(canvas) call. That sets the full canvas view as the layout.

770grappenmaker
  • 292
  • 1
  • 7
  • I make a class that extends View. And I make a Service class that uses the View object via WindowManager. TTTTT So I can call setContentView method because of not Activity. – Hee Joong-Dominic Kim Mar 15 '20 at 13:47
0

Your issue is that by default, XML uses a thing called Titlebar, as answered here. Along with removing the Titlebar, you can set your activity to full-screen and also remove the status-bar by using this solution.

Make sure that your activity imports from Activity rather than ActionBarActivity.

Also, it is better to avoid using actual numbers when setting a position or a size, and especially when the goal is to cover the entire screen. You can get the width and height of the screen by using getWidth() and getHeight() accordingly:

canvas.drawRect(0, 0, getWidth(), getHeight());

Make sure to use these methods only in or after the onDraw method was called, else their values will be equal to 0 since the canvas was not yet initialized.

Eldar B.
  • 1,097
  • 9
  • 22