1

I'm trying to create very similar layout like Google Play Books. I need to create an PopupMenu or PopupWindow, which will be displayed at the same position like in the picture below.

PopupWindow (or PopupMenu)

I am not sure, if it is PopupMenu or PopupWindow or something else. This "window" is displayed when I click on the item from menu.

In case, that I change an orientation to landscape mode, in ActionBar is a new icon, which displays this "window". The picture below shows this situation:

PopupWindow (or PopupMenu) in landscape

I have two questions:

  1. Is this a PopupMenu, PopupWindow or something else?
  2. How can I determine the position of this "window" in case, that it is the PopupWindow and I need to show it?
gotwo
  • 663
  • 8
  • 16
PetrS
  • 1,110
  • 14
  • 38
  • 1
    I always preferred to use a PopupWindow, you can fully customize it. – luiscosta Jul 23 '14 at 14:36
  • Ok, thanks. And can you tell me, please, how can I determine the position of PopupWindow? In the pictures, the PopupWindow is always in the different position. It depends on orientation of screen. – PetrS Jul 23 '14 at 14:38
  • http://codingaffairs.blogspot.com/2017/05/popupwindow-android-using-cardview.html – Developine May 03 '17 at 12:30

1 Answers1

4

You have to create an OnClickListener like this:

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        int[] location = new int[2];
        v.getLocationOnScreen(location);

        // Initialize the Point with x, and y positions
        Point point = new Point();
        point.x = location[0];
        point.y = location[1];

        popupWindow(v, point);

    }

});

The method for popupWindow(View view, Point point):

private ArrayAdapter<String> adapterTypeSelection;

private void popupWindow(View v, Point p) {
    // int popupwindowWidth =
    // UnitConverterClass.convertDpToPx(180,getActivity());
    int popupwindowHeight = LinearLayout.LayoutParams.WRAP_CONTENT;

    LayoutInflater layoutInflater = (LayoutInflater) getActivity()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = layoutInflater.inflate(
        R.layout.dashboard_profile_popup_window, null);

    // Creating the PopupWindow
    final PopupWindow pwindow = new PopupWindow(getActivity());
    pwindow.setContentView(layout);
    // pwindow.showAsDropDown(v);
    // pwindow.setWidth(popupwindowWidth);
    pwindow.setHeight(popupwindowHeight);
    pwindow.setFocusable(true);

    String[] types = {"hello", "hi"};

    adapterTypeSelection = new ArrayAdapter<String>(getActivity(),
        R.layout.<your layout for the popupwindow>,
        R.id.textView, types);

    ListView listview = (ListView) pwindow.getContentView().findViewById(
        R.id.listview_popwindow);

    listview.setAdapter(adapterTypeSelection);

    listview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

            TextView temp = (TextView) parent.getChildAt(position)
                .findViewById(R.id.textView);

            if (temp.getText()
                .toString()
                .equals("hello"))) {
                    //hello
            } else {
                //hi
            }

            pwindow.dismiss();
        }
    });

    pwindow.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss() {
            //TODO dismiss settings
        }

    });

    pwindow.setWidth(<width>);
    pwindow.setBackgroundDrawable(<resource for background>);

    // int OFFSET_X = UnitConverterClass.convertDpToPx(180, getActivity());
    // int OFFSET_Y = UnitConverterClass.convertDpToPx(30, getActivity());
    // pwindow.showAtLocation(layout, Gravity.NO_GRAVITY, 
    //                                p.x + OFFSET_X, p.y + OFFSET_Y);
    pwindow.showAtLocation(layout, Gravity.NO_GRAVITY, p.x, p.y);
}

Hope this helps to make a PopupWindow in the position you want to show it.

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
luiscosta
  • 855
  • 1
  • 10
  • 16
  • Thanks for your answer. Now I know that I have to use PopupWindow. But still I cannot make the PopupWindow in the position I want, because in your code, the PopupWindow is displayed when click on the button. I need to show it if I click on the item from menu. – PetrS Jul 23 '14 at 14:49
  • You have to set the OnClickListener for the items. – luiscosta Jul 23 '14 at 14:52
  • But `MenuItem` does not have `OnClickListener`, right? – PetrS Jul 23 '14 at 14:57
  • Try this: http://stackoverflow.com/a/17396896/3465623 . Then, on the method you just put what I have on my `OnClickListener`. – luiscosta Jul 23 '14 at 15:01
  • http://codingaffairs.blogspot.com/2017/05/popupwindow-android-using-cardview.html – Developine May 03 '17 at 12:31