Home » Android » android – PopupView not showing up?

android – PopupView not showing up?

Posted by: admin May 14, 2020 Leave a comment

Questions:

Here’s the XML (just a webview):

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/couponView" android:layout_height="100dp" android:layout_width="100dp" />

and the code:

final View cView = getLayoutInflater().inflate(R.layout.couponlayout, null);
PopupWindow pw = new PopupWindow(cView);
pw.showAtLocation(findViewById(R.id.mainLayout), Gravity.CENTER, 100, 100);
pw.update();

This is in a button.onClick() method. When I click the button, the rest of the things that should happen (button changes color, text, etc.), but the PopupWindow doesn’t show up. I’ve been combing the web but can’t find any fixes. What am I doing wrong?

edit: no one knows whats going on? I feel like this is a common problem.

How to&Answers:
PopupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

NOTE: setWindowLayoutMode has been deprecated. Use setHeight and setWidth.

Answer:

The PopupWindow probably has a width and height of 0 since the dimensions weren’t initialized, which would explain why you can’t see anything. You can set the height and width with setHeight and setWidth, or do it with the constructor PopupWindow(View contentView, int width, int height).

Check the Android reference for more info.