0

I want to create a popup menu same as that of Play Store. The popup menu should open in bottom right corner of grid view item. But if the item is in bottom of screen, the popup should open above the point where it's clicked.

I have tried popup menu, but it opens either below or top of the item.

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

        System.out.println("position is : " + position);
        final MenuItems menuItems = (MenuItems)parent.getItemAtPosition(position);
        final String default_topping_link = menuItems.getLink();
        try{
            topping = DefaultToppingParser.parse(new FileInputStream(default_topping_link));
            for(int i=0;i<topping.size();i++){
                System.out.println("topping id  : " + topping.get(i));
            }
        }catch(Exception e){
            e.printStackTrace();
        }

        if(position == (burger_item_AL.size()-1)){
            Intent intent = new Intent(MainActivity.this,CustomiseItem.class);
            intent.putExtra("default_toppings_id", base_id);
            System.out.println("intent");
            startActivity(intent);
        } else {
            PopupMenu popupMenu = new PopupMenu(MainActivity.this, view);
            popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
            popupMenu.show();

            popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {

                    ....some coding

                    return true;
                }
            });
        }
    }
});

I have gone through many popup menu on Google but couldn't find same as that of Play Store. Can anyone suggest some solution?

popup window:

In adapter class:

@Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_DOWN){
                    MainActivity.p = new Point();
                    MainActivity.p.x = (int) event.getRawX();
                    MainActivity.p.y = (int) event.getRawY();
                    if (MainActivity.p != null){
                            MainActivity.position = position;
                            MainActivity.showPopup(context, MainActivity.p);
                        }
                    return true;
                }
                return false;
            }
        });

showpopup method:

 public static void showPopup(final Context context, Point p) {
           int popupWidth = 150;
           int popupHeight = 150;

           // Inflate the popup_layout.xml
           LinearLayout viewGroup = (LinearLayout) ((Activity) context).findViewById(R.id.popup);
           LayoutInflater layoutInflater = (LayoutInflater) context
             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View layout = layoutInflater.inflate(R.layout.popup, viewGroup);

           // Creating the PopupWindow
           final PopupWindow popup = new PopupWindow(context);
           popup.setContentView(layout);
           popup.setWidth(popupWidth);
           popup.setHeight(popupHeight);
           popup.setFocusable(true);

           // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
           int OFFSET_X = 30;
           int OFFSET_Y = 30;

           // Clear the default translucent background
           popup.setBackgroundDrawable(new BitmapDrawable());

           // Displaying the popup at the specified location, + offsets.
           System.out.println("showing popup");
           popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x - OFFSET_X, p.y + OFFSET_Y);
           }
megha
  • 301
  • 5
  • 18

2 Answers2

0

You should use a PopupWindow to mimic the behaviour you are interested in.

Following this link you can find an interesting tutorial that teaches how to achieve that. http://mrbool.com/how-to-implement-popup-window-in-android/28285

Hope it helps.

AlexBalo
  • 1,258
  • 1
  • 11
  • 16
0

main.xml

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="@+id/spam"
    android:title="Spam"/>
<item
    android:id="@+id/blockuser"
    android:title="Block User"/>
<item
    android:id="@+id/remove"
    android:title="Remove"/>

popup menu code

PopupMenu popup = new PopupMenu(activity, v);

            /** Adding menu items to the popumenu */
            popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());
            popup.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    // TODO Auto-generated method stub
                    switch (item.getItemId()) {
                    case R.id.spam:

                        Toast.makeText(activity, "Spam clicked",
                                Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.blockuser:
                        Toast.makeText(activity, " Block user clicked",
                                Toast.LENGTH_SHORT).show();
                        break;

                    case R.id.remove:
                        Toast.makeText(activity, "Remove clicked",
                                Toast.LENGTH_SHORT).show();

                        // RecentFragment rf = new RecentFragment();
                        // rf.onItemClick(position);
                        break;

                    default:
                        break;
                    }

                    return false;
                }
            });
            popup.show();

this code is working for me perfectly and my requirement is same as u mentioned in your description hope it will help you

  • check my first code. I have tried popup menu. It displays menu either at bottom or on top.But I want the menu to be on bottom right or if right most item is clicked the it should align to base but open towards center of screen – megha Jul 30 '14 at 10:13
  • ohhh actually popup menu shows its menu as per ROOM (space) available for its menu i hope it may help you anyways try google you can get ur answer happy coding – Android is everything for me Jul 30 '14 at 10:16
  • i have searched google but couldn't find any solution – megha Jul 30 '14 at 10:18