2424import android .os .Handler ;
2525import android .os .Looper ;
2626import android .util .Log ;
27+ import android .view .Gravity ;
2728import android .view .LayoutInflater ;
2829import android .view .View ;
2930import android .view .ViewGroup ;
@@ -270,7 +271,7 @@ public void onClick(View v) {
270271 linksBox .setVisibility (View .GONE );
271272 }
272273
273- popup . showAsDropDown ( anchor );
274+ showPopupSmart ( popup , anchor );
274275 } catch (Exception e ) {
275276 Log .e (TAG , "showPopup failed: " + e .getMessage ());
276277 }
@@ -284,6 +285,26 @@ private static boolean isDebuggable(Context context) {
284285 }
285286 }
286287
288+ // Show the popup below the anchor, or above it when the anchor sits low on
289+ // screen (e.g. a bottom navigation/close button) so it isn't clipped off the
290+ // bottom. Anchor-position based, so it works even while the WebView content
291+ // is still laying out.
292+ private static void showPopupSmart (PopupWindow popup , View anchor ) {
293+ try {
294+ int screenH = anchor .getResources ().getDisplayMetrics ().heightPixels ;
295+ int [] loc = new int [2 ];
296+ anchor .getLocationOnScreen (loc );
297+ int anchorTop = loc [1 ];
298+ if (anchorTop > screenH * 0.55f ) {
299+ popup .showAtLocation (anchor , Gravity .BOTTOM | Gravity .START , loc [0 ], screenH - anchorTop );
300+ } else {
301+ popup .showAsDropDown (anchor );
302+ }
303+ } catch (Exception e ) {
304+ try { popup .showAsDropDown (anchor ); } catch (Exception ignored ) {}
305+ }
306+ }
307+
287308 public static void dismissActive () {
288309 try {
289310 if (activePopup != null ) {
0 commit comments