Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PopupWindow is above status bar in some device

Here is my code for show a PopupWindow

View rootView = activity.getWindow().getDecorView().getRootView();
mPopupWindow.showAtLocation(rootView, Gravity.NO_GRAVITY, 0, 0);

When I test it on Samsung, Nexus device it show below the status bar
However when I test it on Sony device it show above the status bar (like the image below)

So which device display correct or what I do wrong ? Any help or suggestion would be great appreciated

enter image description here

like image 445
Linh Avatar asked Jan 03 '17 09:01

Linh


People also ask

How do I show a popup above a view?

To display the popup window above the view, you can use the showAsDropDown() function. With: v: View(Anchor) 0: offSetX.

What is pop up window in Android?

android.widget.PopupWindow. This class represents a popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.

How to hide statusbar in Android studio?

Hide the Status Bar on Android 4.View decorView = getWindow(). getDecorView(); // Hide the status bar. // status bar is hidden, so hide that too if necessary.


1 Answers

I solve my problem by create a temp View at top-left (topLeftView in below code).
Then when I show the PopupWindow, I will show it at (0, the y coordinate of topLeftView) not (0,0)

View topLeftView = findViewById(R.id.top_left_View);
int topLeftPos[] = new int[2];
topLeftView.getLocationOnScreen(topLeftPos);

mPopupWindow.showAtLocation(topLeftView, Gravity.NO_GRAVITY, 0, topLeftPos[1]);

I think this solution will work for all devices

like image 122
Linh Avatar answered Oct 04 '22 04:10

Linh