Reputation: 692
I am using the following code script to do dim background for Android PopupWindow.It is working well for the android version except version 6 (Marshmallow).
It throws "Cannot cast FrameLayout.LayoutParams to WindowManager.LayoutParams"
How to solve this problem for Android version 6.I dont want to use Dialog.
PopupWindow popup = new PopupWindow(contentView, width, height);
popup.setBackgroundDrawable(null);
popup.showAsDropDown(anchor);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams p = (WindowManager.LayoutParams) contentView.getLayoutParams();
p.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
p.dimAmount = 0.3f;
wm.updateViewLayout(contentView, p);
Upvotes: 2
Views: 1302
Reputation: 409
This works for me on Android M and above:
public static void controlPopupDim(PopupWindow popupWindow, boolean showing) {
View parent = popupWindow.getContentView().getRootView();
Context context = parent.getContext();
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams wlp = (WindowManager.LayoutParams) parent.getLayoutParams();
wlp.flags |= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
wlp.dimAmount = showing ? 0.5f : 0f;
if (wm != null) {
wm.updateViewLayout(parent, wlp);
}
}
You basically need to call it with true
when you call the showAsDropDown
on your popup and false
when the popup is dismissed (may be via the onDismissListener
)
Upvotes: 0
Reputation: 584
you can use custome dialog instead of popup window. popup window create problem in android 6. You can do everything like popup window in custom dialog. just create a layout and inflate into dialog and you will get every property of dailog. here is code.
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //for remove default title
dialog.setCancelable(false); //for canceal back button click
dialog.setContentView(R.layout.payment_popup_window);
ImageButton variable_name=(ImageButton)dialog.findViewById(R.id.xml_id);
ImageButton variable_name2=(ImageButton)dialog.findViewById(R.id.xml_id2);
Button variable_name3=(Button)dialog.findViewById(R.id.xml_id3);
variable_name1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your code
dialog.dismiss();
}
});
variable_name2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//your code
dialog.dismiss();
}
});
variable_name3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //for making dialog fit in android screen
Along this outside touch can't dismiss it. you can't get this in popup window in android 6.
Upvotes: 1
Reputation: 399
I also faced the same issue. For Marshmallow and above, you need to add one more getParent() to access the container. Use the code below:
if (android.os.Build.VERSION.SDK_INT > 22) {
contentView = (View) pwindow.getContentView().getParent().getParent();
} else {
contentView = (View) pwindow.getContentView().getParent();
}
Upvotes: 0