1

I am trying to implement a ListPopUpWindow something like the twitter like Menu Popup. But then it's stuck to the right side of the screen. Any help would be appreciated thanks. My code's below

PopUpWindow Method

 private void onListPopUp(View anchor) {

    ArrayList<Person> persoonItem = new ArrayList<>();
    persoonItem.add(new Person(R.drawable.ic_profile_logo, "Oladeji Abubakar", "abubakar.oladeji@deliveryscience.co", ""));
    persoonItem.add(new Person(0, "", "", "Update Phone"));
    persoonItem.add(new Person(0, "", "", "Your Cards"));
    persoonItem.add(new Person(0, "", "", "Invite Friends"));
    persoonItem.add(new Person(0, "", "", "Logout"));

    ListPopupWindowAdapter mListPopUpWindowAdapter = new ListPopupWindowAdapter(getApplicationContext(), persoonItem);

    final ListPopupWindow pop = new ListPopupWindow(this);
    pop.setAdapter(mListPopUpWindowAdapter);
    pop.setAnchorView(anchor);
    pop.setModal(true);
    pop.setContentWidth(toolbar.getWidth() / 2);
    pop.setHeight(ListPopupWindow.WRAP_CONTENT);
    pop.setVerticalOffset(-105);
    //pop.setHorizontalOffset(100);
    //pop.setPromptPosition(20);
    //pop.setHorizontalOffset();
    pop.setPromptPosition(0);
    pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            pop.dismiss();
            Toast.makeText(TestActivity.this, "Clicked" + ((Person) parent.getItemAtPosition(position)).getOthers(), Toast.LENGTH_LONG).show();
        }
    });
    pop.show();
    WindowManager.LayoutParams wlp = new WindowManager.LayoutParams(toolbar.getWidth()/2, LinearLayout.LayoutParams.WRAP_CONTENT);
    wlp.gravity = Gravity.TOP | Gravity.END;
    wlp.horizontalMargin = -20;
    wlp.x = -20;
    View parentView = (View) pop.getListView().getParent();
    parentView.setLayoutParams(wlp);
}

And the image

Image

This is what I'm trying to achieve

enter image description here

Tonespy
  • 3,257
  • 7
  • 26
  • 52
  • does this popup has an xml file ?? please show your layout also – Fakher Sep 02 '15 at 07:55
  • Nope. Just the ListItems has have two XML. One for the first row and the second for the remaining of the rows – Tonespy Sep 02 '15 at 07:56
  • ok, you want from your popup to be shown in the middle ?? – Fakher Sep 02 '15 at 07:57
  • Nope. Just let's say 10-20 margin before the right. I've edited my question – Tonespy Sep 02 '15 at 08:04
  • i think that the best solution is to make your own popup, so you can manage it as you can. You can find a lot of tutorial about that ;) – Fakher Sep 02 '15 at 08:43
  • Well, I am making mine using a ListPopWindow. It's not the normal Menu Item – Tonespy Sep 02 '15 at 08:48
  • you make it without xml file ?? ok if it's only java code than you must change some attributes to be placed in the choosen area ;) – Fakher Sep 02 '15 at 08:50
  • I'm facing the same issue. I am trying to write a custom dropdown list for a toolbar however I can't seem to shift the dropdown layout from the right-hand edge of the screen. Did you find a solution? – Charlie Feb 23 '17 at 11:01

1 Answers1

3

You can offset the ListPopupWindow by using ListPopupWindow#setHorizontalOffset and ListPopupWindow#setVerticalOffset.

What is confusing is that if your anchor is positioned on the right of the screen you need to add the width value of your ListPopupWindow to the horizontal offset while the vertical offset is always 0 based. Using values below the width for the horizontal offset when the ListPopupWindow is anchored to the right seems to not have any effect. Also positive in the horizontal axis is pointing to the right so we invert the horizontal value.

The following Code positions the ListPopupWindow 5dp down and 5dp to the left from an anchor positioned on the top right:

  final ListPopupWindow popupWindow = new ListPopupWindow(context);

  int widthPixels = convertDpToPixels(200, context);
  int horizontalOffset = -(widthPixels + convertDpToPixels(5, context));
  int verticaloffset = convertDpToPixels(5, context);

  popupWindow.setAnchorView(anchor);
  popupWindow.setWidth(widthPixels);
  popupWindow.setHorizontalOffset(horizontalOffset);
  popupWindow.setVerticalOffset(verticaloffset);