0

I want that my pop up menu appear next to the clicked row. Look at the picture after I click the "..." button: http://postimg.org/image/s35znsaq3/

I added my adapter that init each row. When I click on moreActionsButton, the listener call the method showPopUpMenu that open the menu. Inside the menu I will do some staff.

This is my code for the Pop Up Menu:

public class ContactsListActivity extends Activity {

      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        // Set layout for this activity
        setContentView(R.layout.contacts_list);


        // Connect between buttons to layout id
        list = (ListView) findViewById(R.id.my_user_list);
        spinner = (ProgressBar) findViewById(R.id.spinner);

        // Setting adapter and creating contacts list
        if (selectedGroupId != null) {
            Model.getInstance().getAllUsersByGroupId(selectedGroupId, new Model.userListReturnedListener() {
                @Override
                public void userListReturned(List<User> userList) {
                    spinner.setVisibility(View.GONE);
                    data = userList;
                    adapter = new MyAdapter();
                    list.setAdapter(adapter);
                    adapter.notifyDataSetChanged();
                }
            });
        }


        class MyAdapter extends BaseAdapter {

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;

            if (convertView == null) {
                // Creates a ViewHolder and store references to layouts we want to bind data to.
                holder = new ViewHolder();
                LayoutInflater inflater = getLayoutInflater();
                convertView = inflater.inflate(R.layout.contacts_row, null);

                // Connect between buttons to layout id
                holder.contactName = (TextView) convertView.findViewById(R.id.contactName);
                holder.contactImage = (ImageView) convertView.findViewById(R.id.contactImage);
                holder.moreActionsButton = (Button) convertView.findViewById(R.id.moreActions);

                //Save holder
                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back
                holder = (ViewHolder) convertView.getTag();
            }

            // Setting user
            final User us = data.get(position);
            holder.contactName.setText(us.getFname() + " " + us.getLname());


           holder.moreActionsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopUpMenu(us);
            }
        });


        // Setting the popUpMenu buttons
        popUpMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_skype:
                        AppsCommunication.onSkypeClick(ContactsListActivity.this, user.getSkype());
                        break;

                    case R.id.1:
                        //Do something
                        break;

                    case R.id.2:
                     //Do something
                        break;

                    case R.id.3:
                    //Do something
                    default:
                        break;

                }
                return true;
            }
        });

        Object menuHelper;
        Class[] argTypes;
        try {
            Field fMenuHelper = PopupMenu.class.getDeclaredField("mPopup");
            fMenuHelper.setAccessible(true);
            menuHelper = fMenuHelper.get(popUpMenu);
            argTypes = new Class[]{boolean.class};
            menuHelper.getClass().getDeclaredMethod("setForceShowIcon", argTypes).invoke(menuHelper, true);
        } catch (Exception e) {
            // Possible exceptions are NoSuchMethodError and NoSuchFieldError
            //
            // In either case, an exception indicates something is wrong with the reflection code, or the
            // structure of the PopupMenu class or its dependencies has changed.
            //
            // These exceptions should never happen since we're shipping the AppCompat library in our own apk,
            // but in the case that they do, we simply can't force icons to display, so log the error and
            // show the menu normally.
        }

        popUpMenu.show();
    }    

    // Class to handle row buttons
    class ViewHolder {
        private TextView contactName;
        private ImageView contactImage;
        private ImageButton saveContactButton;
        private ImageButton callButton;
        private ImageButton smsButton;
        private Button moreActionsButton;
        private ProgressBar contactImageProgressbar;
    }
}
Alex Tech
  • 123
  • 1
  • 9
  • Try with constructor PopupMenu (Context context, View anchor, int gravity) and set gravity with Gravity.CENTER – fechidal89 Jun 08 '16 at 22:30
  • @fechidal89 I updated my code. It doesn't work. The pop up menu jump to other line, I want it near to the moreActions button – Alex Tech Jun 08 '16 at 22:46
  • I think that is better use Intent Action (https://developer.android.com/training/sharing/send.html?hl=es) for show options "to share" or use PopupWindow (http://stackoverflow.com/questions/7440334/how-do-gravity-values-effect-popupwindow-showatlocation-in-android) instead of create an Menu by row. – fechidal89 Jun 08 '16 at 23:12
  • That wasn't my question but thanks. I don't share text. I open each app and comunicate with phone/username so Action.Send is not the case. @fechidal89 – Alex Tech Jun 08 '16 at 23:14
  • Put more information (list adapter, layouts or file xml, etc). I think that problem is that you have one reference of menu and when you invoques the function findViewById, this return the first. You can use the ViewHolder Pattern (https://www.codeofaninja.com/2013/09/android-viewholder-pattern-example.html) – fechidal89 Jun 09 '16 at 03:21
  • @fechidal89 I added my adapter and my pop up menu method, I think you are right, there is a problem with the adapter... – Alex Tech Jun 09 '16 at 23:04

0 Answers0