Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PopupWindow - not working on a few devices

I use following code to show a small popup:

public static PopupWindow showImportMenu(Activity activity, View anchor, PopupWindowClickListener onClickListener)
{
    LayoutInflater inflater = LayoutInflater.from(activity);

    PopupImportBinding binding = DataBindingUtil.inflate(inflater, R.layout.popup_import, null, false);

    if (!RootTools.isRootAvailable())
        binding.llImportRootMethod.setVisibility(View.GONE);


    PopupWindow popupWindow = new PopupWindow(activity, null, R.attr.popupMenuStyle);
    popupWindow.setFocusable(true);
    popupWindow.setContentView(binding.getRoot());
    popupWindow.setOutsideTouchable(true);
    PopupWindowCompat.showAsDropDown(popupWindow, anchor, 0, 0, Gravity.BOTTOM);

    View.OnClickListener clickListener = new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            onClickListener.onClick(popupWindow, view);
        }
    };

    binding.llImportDefault.setOnClickListener(clickListener);
    binding.llImportRootMethod.setOnClickListener(clickListener);
    binding.llImportHTCFromContacts.setOnClickListener(clickListener);
    binding.llImportManual.setOnClickListener(clickListener);

    return popupWindow;
}

This works on a lot of devices but on some rare devices it does not work, like:

  • Android 5.1.1 root slim rom
  • maybe others... until now, I don't know more about other devices

I got the feedback that no popup is shown. Does anyone know why this is not working on the above mentioned device? And what I can do to make it work on this device as well?

EDIT

It seems like it's not clear that what I want is following:

  • use showAsDropDown not showAtLocation or similar, I never saw this problem with showAtLocation yet
  • my solution is working on nearly all devices, it seems to be a phone/rom specific problem, maybe it's not even solvable as it COULD be a bug in the device as well => if someone knows of such a bug, telling me would be fine as well
  • I don't want to use a dialog (or anything else) instead, that's not answering my question. I currently use a BottomSheet which is fine for me, but still I would like to know if the problem can be solved and somehow handled
like image 437
prom85 Avatar asked Apr 10 '17 19:04

prom85


3 Answers

I got the same problem on a Nexus 7 (not 2012) running the 5.1.1. It is finally fixed by adding this line:

popupWindow.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
like image 165
Chris Avatar answered Oct 19 '22 12:10

Chris


I was having the same problem: my PopupWindow was not showing on my 5.1.1 android device but it was on others. I realised that I had to specify the width and height in order to be shown on that version (which is still compatible with the rest of the versions as well).

Here is an example:

popUp.setWidth(MATCH_PARENT);
popUp.setHeight(WRAP_CONTENT);
like image 4
RedDeath Avatar answered Oct 19 '22 13:10

RedDeath


In my case, popup window have no sizes on few devices.

Try that after setContentView

50000 - just a big size for measure.

    popupWindow.getContentView().measure(50000, 50000);
    popupWindow.setWidth(popupWindow.getContentView().getMeasuredWidth());
    popupWindow.setHeight(popupWindow.getContentView().getMeasuredHeight());

You can use screen size instead 50000

like image 2
Zeon Avatar answered Oct 19 '22 14:10

Zeon