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);
How to show Popup Window in Android. I am using below code but it give fatal exception.
2096
14-Jun-2016
Aryan Kumar
27-Jun-2023Sure, 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
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:
LayoutInflaterclass is used to inflate the popup window layout.PopupWindowclass is used to create and show the popup window.showAtLocation()method is used to show the popup window at a specific location.Manoj Pandey
15-Jun-2016