3

I made a system overlay window that always stays on top of other apps. The width of the screen is 'MATCH_PARENT' and I want from the height to cover a certain area of the screen but my problem is that the height won't be same on different screens.

Here is my code:

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT,
                1500,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN |
                        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSPARENT);

How can can I make that 1500p be the same on different screens? Thanks.

Sruda
  • 53
  • 7

2 Answers2

0

Try to create a dimension resource dimens.xml files with the window height dimension:

<dimen name="window_height">1500dp</dimen>

Then assign the dimension to the window height by calling the getRecources().getDimension() from the context like so:

int height = context.getResources().getDimension(R.dimen.window_height);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            height,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSPARENT);
Grace Coder
  • 784
  • 10
  • 13
  • Just tried it but the gradle-build sends " Error:(5, 5) String types not allowed (at 'window_height' with value '1500p')." how can get past this? – Sruda Jan 01 '16 at 15:32
  • Unfortunately it didn't work, it covers the whole screen. I even got context error in "context.getResouces()..." so I changed it to " (int) getResouces()...". can that be the reason? – Sruda Jan 01 '16 at 15:54
  • 1
    Feel free to reduce the dimension to smaller value such as 300dp and try again and adjust it until the value you wanted. If your window is an Activity then can leave out the context just `(int) getResouces()...` will do – Grace Coder Jan 01 '16 at 16:00
  • I ended up using @tiny sunlight's suggestion it, it was the one I was in need of. But thanks for the help, I appreciate it! – Sruda Jan 02 '16 at 00:21
0

My problem was solved by tiny sunlight:

(int)(getResources().getDisplayMetrics().heightPixels* 0.8f)

you have to replace the desired dimensions parameter with this and adjust the 0,8F to the desired measurements.

Sruda
  • 53
  • 7