Search code examples
androidimageviewonlongclicklistener

onLongClick should only happen if condition is true


This is a part of my code:

        if(user.getUserID().equals(thisLike.getUserID())) {
            Log.i("On MARKER click", "Equal User ID!");
            Toast.makeText(getApplicationContext(), R.string.longpress, Toast.LENGTH_SHORT).show();
            markerImage.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Log.i("On MARKER click", "onLongClick HAPPENS!");
                    if(user.getUserID().equals(thisLike.getUserID())) {
                        Log.i("On MARKER click", "Equal User ID!");
                        openContextMenu(markerImage);
                        return true;
                    } else {
                        Log.i("On MARKER click", "Not equal User ID!!");
                        return false;
                    }
                }
            });
        } else {
            Log.i("On MARKER click", "F...off!");
            return false;
        }

It describes some behavior within onMarkerClick on a GoogleMap Marker.

What I want to happen is for the ContextMenu to open ONLY when user.getUserID() is equal to thisLike.getUserID()

I have tried adding markerImage.setOnLongClickListener(null); on both else statements. Did not work..


Solution

  • While this solution is not perfect... I've added the if-check to onCreateContextMenu like this:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        if(user.getUserID().equals(thisLike.getUserID())) {
    
            menu.setHeaderTitle("Image options");
            getMenuInflater().inflate(R.menu.context_menu_image, menu);
        }
    }
    

    It is finally respecting the if-check, but I would still like to know why the if-check for onLongClick isn't helping.