0

I have a Service that instantiate a ImageView, using WindowManager class, on the screen, this service have no window or visible objects. When I try to set the position of this ImageView using the WindowManager, it set the Y axis position with an offset, this seems to be caused because of the notication bar that the Windows Manager does consider when positioning the ImageView, but the position calculated is absolute and does not consider the Notification Bar size. I can easy subtract this offset from the position, but for this I need to know the Notification Bar size.

I have found some ways to do it, but most of them uses an activity absolute position, or have the notification Bar size hard coded, accordingly with the screen resolution.

Is there a way to discover the Notification Bar size, that I do not need to use hard coded values and do not need any visualizible objects?

user2977500
  • 147
  • 10

1 Answers1

0

Did you try using the following code?

Rect rectangle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight= rectangle.top;

Update
You said the application directly launches the service. What I'd suggest you to is to

  1. Have an activity that opens up when the app is launched.
  2. Get the status bar height in that activity.
  3. Start the service and pass the status bar height.
  4. Close the activity.

This is hacky way, and since essentially you would close the activity as soon as you start the service, I don't think the user will be able to see it as the process will be very fast.

Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • I have seem something like this, the problem is that I don't have a window to getWindow(). Is there something I can to about it? – user2977500 Jul 16 '15 at 01:14
  • You said you have a service. Can't you get the status bar height in your activity and then pass that to your Service? – Antrromet Jul 16 '15 at 01:17
  • I idea is not have an activity on the inicialization, the application launches directly a service. – user2977500 Jul 16 '15 at 01:21
  • I am trying to do something like this, with an ImageView, but when I get its position, it returns me relative position(0,0). I am using getLocationOnScreen. – user2977500 Jul 16 '15 at 01:37
  • You might want to try [getLocationOnWindow](http://developer.android.com/reference/android/view/View.html#getLocationInWindow%28int%5B%5D%29) – Antrromet Jul 16 '15 at 01:41
  • Neither have worked. I don´t understand why. On Google documantion is: Computes the coordinates of this view on the screen. The argument must be an array of two integers. After the method returns, the array contains the x and y location in that order. It should return a absolute position, shouldn't it? – user2977500 Jul 16 '15 at 01:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/83376/discussion-between-user2977500-and-antrromet). – user2977500 Jul 16 '15 at 01:58