0

I have been struggling to figure out why my PopupWindow refuses to show up. I have inflated its view and made sure that it is appearing in the correct view container.

Any ideas on why the window doesn't show up?

Here is the code that I call in the activity where I want the popup:

    LinearLayout linearLayout = findViewById(R.id.webLinLayout);
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    ViewGroup container = (ViewGroup) inflater.inflate(R.layout.activity_popup_window, null);
    boolean focusable = false;
    PopupWindow popupWindow = new PopupWindow(container, 500, 500, focusable);
    popupWindow.showAtLocation(linearLayout, Gravity.CENTER, 0, 0);

The XML file for my activity layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".WebSearchActivity"
    android:background="@color/semitransparent"
    android:id="@+id/webLinLayout">

    <EditText
        android:id="@+id/imgSearchBar"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:maxLines="1"
        android:textAlignment="textStart"
        android:hint="Search for an image">
    </EditText>

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="15dp"
        android:clickable="true">
    </android.support.v7.widget.CardView>

</LinearLayout>

And here is my popup layout:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/loadingMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="Fetching your images..."
        android:textSize="48sp"/>

    <android.support.v4.widget.ContentLoadingProgressBar
        android:layout_width="300dp"
        android:layout_height="300dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/loadingMsg" />

</android.support.constraint.ConstraintLayout>
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Which lifecycle are you trying to show PopupWindow in? Is [this article](https://stackoverflow.com/questions/14730281/android-popup-window-call-from-oncreate/14731002) helpful? – ysys Mar 08 '19 at 23:14

1 Answers1

0

try this:

final LinearLayout linearLayout = findViewById(R.id.webLinLayout);

LayoutInflater inflater =
        (LayoutInflater)
                getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);

final ViewGroup container =
        (ViewGroup) inflater.inflate(R.layout.activity_popup_window, null);

boolean focusable = false;

final PopupWindow popupWindow = new PopupWindow(container, 500, 500, focusable);
linearLayout.post(new Runnable() {
    public void run() {
        popupWindow.showAtLocation(linearLayout, Gravity.CENTER, 0, 0);
    }
});
med.Hamdan
  • 190
  • 1
  • 6
  • It seems to work! What do you think the issue was with trying to get the popup to work without creating a post thread? – Kivalu Ikaika Ramanlal Mar 08 '19 at 23:34
  • in LogCat the error was Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? This error happens when you are trying to show popUpWindow too early, you need to call showAtLocation after all views are fully drawn https://stackoverflow.com/questions/3947145/what-event-is-fired-after-all-views-are-fully-drawn – med.Hamdan Mar 09 '19 at 08:04