16

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
prom85
  • 16,896
  • 17
  • 122
  • 242
  • 1
    Do you have stacktraces from that devices? – azizbekian Apr 13 '17 at 07:59
  • 1
    there's no crash on this device, it just does not show anything. But I requested the logcat, did not get one yet though – prom85 Apr 13 '17 at 12:30
  • 1
    At least some system logs should appear. – azizbekian Apr 13 '17 at 12:30
  • 1
    why don't you ask him/her for video so you could check better way and would get good idea that what he/she doing and that is cause – Drim Apr 17 '17 at 13:14
  • Did you used showAtLocation? In some devices I saw that showAsDropDown is giving anchor position zero. – androidcodehunter Apr 19 '17 at 12:09
  • No. My experience is that `showAtLocation` is working as long as you provide a background drawable but I have not tried it on this special device because the user is not answering anymore. I want to have the advantages of `showAsDropDown` because my popup may need to be drawn on top, left, rigt, bottom or even overlaying the anchor depending on scroll position and screen size. Doing everything manually probably will work, but that's just an assumption, because in another app I never had problems with the `PopupWindow` and there I use `showAtLocation`... – prom85 Apr 19 '17 at 12:13
  • I'm facing the same issue. Good new is I have the device where issue can be reproduced. I can confirm that `showAtLocation()` does not help. `PopupWindow` is still not displayed. This is an Android 5.0.2 device from Vivo. – Prasad Pawar Apr 08 '19 at 09:19

6 Answers6

7

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);
Chris
  • 704
  • 7
  • 10
4

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);
RedDeath
  • 283
  • 4
  • 17
2

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

Zeon
  • 535
  • 8
  • 23
1

Some ROMs restrict usage of popupview with their own permissions. So user have to explicitly turn on permission to show pop up views.

Even MIUI will restrict popupview to be displayed by default.

Please have a look whether there is any permission in that ROMs or devices.

Sangeet Suresh
  • 2,527
  • 1
  • 18
  • 19
  • That's the only useable answer, thanks. Does this permission have a name? Can it be requested in the manifest or at runtime? Or can those people only manually give this permission somewhere in their system? Is it possible to test if the Rom needs permissions for a popup view? It's still weird that popup menus work though without problems. – prom85 Apr 20 '17 at 06:07
  • That kind of permission is not included in android OS. So we can't use android requestpermission method to grant runtime permission. One option is that, we can navigate user to that permission screen. – Sangeet Suresh Apr 20 '17 at 08:03
0

enter image description here## Ok i implemented popupwindow for sorting in my tab fragment and i checked working fine once try this

I used in that custom layout for popup window

  final PopupWindow popupWindow = new PopupWindow(getActivity());
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = inflater.inflate(R.layout.popmenu1t1, null);
            l8[![enter image description here][1]][1] = (LinearLayout) view.findViewById(R.id.atoz);


  l8.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    date_sort="0";
                    discount_sort="";
                    price_sort="";
                    alpha_sort="";
                    popupWindow.dismiss();
                }
            });
            int width = 900;
            int height = 400;
            try {
                WindowManager wm = (WindowManager)view.getContext().getSystemService(Context.WINDOW_SERVICE);
                width = wm.getDefaultDisplay().getWidth();
                height = wm.getDefaultDisplay().getHeight();
            } catch (Exception e) {
                e.printStackTrace();
            }
            popupWindow.setWidth(width*3/6);
            popupWindow.setFocusable(true);
            popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
            popupWindow.setContentView(view);
            popupWindow.setBackgroundDrawable(null);
            popupWindow.setOutsideTouchable(true);
            popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

Find the below attached screen shot popupwindow in my app

Venkatesh
  • 1,034
  • 13
  • 25
0

I had created a custom popup dialog shows at bottom of screen

public class MoreOptionDialog {

private Dialog dialog;
private Context context;
private int size;

public MoreOptionDialog(Context context) {
    this.context = context;    
}

public void showMoreOptionDialog(List<String> listMoreOption) {

    dialog = new Dialog(new ContextThemeWrapper(context, R.style.DialogSlideAnim));
    dialog.getWindow().setWindowAnimations(R.style.DialogSlideAnim);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    View view = View.inflate(context, R.layout.dialog_more_option, null);

    dialog.setContentView(view, new LinearLayout.LayoutParams(utility.getScreenWidth() - 100, size));
    dialog.getWindow().setGravity(Gravity.BOTTOM);

    ListView listView = (ListView) view.findViewById(R.id.lvMoreOption);

    MoreOptionAdapter moreOptionAdapter = new MoreOptionAdapter(context, listMoreOption, Gravity.CENTER);
    listView.setAdapter(moreOptionAdapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            dialog.dismiss();                
        }
    });

    dialog.show();
}
}

And style is here

<style name="DialogSlideAnim">
    <item name="android:windowAnimationStyle">@style/DialogAnimation</item>
    <item name="android:windowBackground">@color/color_white</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
</style>

And this dialog working in all devices :)

Anand Savjani
  • 2,484
  • 3
  • 26
  • 39