---
title: "How to show Popup Window in Android. I am using below code but it give fatal exception."  
description: "How to show Popup Window in Android. I am using below code but it give fatal exception."  
author: "Anonymous User"  
published: 2016-06-14  
updated: 2023-06-27  
canonical: https://www.mindstick.com/forum/34137/how-to-show-popup-window-in-android-i-am-using-below-code-but-it-give-fatal-exception  
category: "android"  
tags: ["android"]  
reading_time: 2 minutes  

---

# How to show Popup Window in Android. I am using below code but it give fatal exception.

```
  PopupWindow popupWindow = new PopupWindow(getApplicationContext());                // inflate your layout or dynamically add view        LayoutInflater inflater = (LayoutInflater) getWindow().getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);        View view = inflater.inflate(R.layout.my_layout, null);        popupWindow.setFocusable(true);        popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);        popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);        popupWindow.setContentView(view);              popupWindow.showAsDropDown(v);
```

## Replies

### Reply by Aryan Kumar

Sure, I can help you with that.

The code that you are using to show a popup window in Android is missing some important elements. Here is the corrected code:

Code snippet

```plaintext
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Inflate the popup window layout
        LayoutInflater inflater = getLayoutInflater();
        View popupView = inflater.inflate(R.layout.popup_window, null);

        // Create the popup window
        PopupWindow popupWindow = new PopupWindow(popupView, 500, 500);

        // Show the popup window
        popupWindow.showAtLocation(findViewById(R.id.main_layout), Gravity.CENTER, 0, 0);
    }
}
```

This code will create a popup window that is 500 pixels wide and 500 pixels high. The popup window will be displayed in the center of the main layout.

The following are some of the important elements that were missing from your original code:

- The `LayoutInflater` class is used to inflate the popup window layout.
- The `PopupWindow` class is used to create and show the popup window.
- The `showAtLocation()` method is used to show the popup window at a specific location.

### Reply by Manoj Pandey

Hi Linda Use below code PopupWindow popupWindow = new PopupWindow(getWindow().getContext());\
\
instead of PopupWindow popupWindow = new PopupWindow(getApplicationContext());\


---

Original Source: https://www.mindstick.com/forum/34137/how-to-show-popup-window-in-android-i-am-using-below-code-but-it-give-fatal-exception

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
