Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8308261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:45:11+00:00 2026-06-08T18:45:11+00:00

I have a list view which has been registered for a context menu. For

  • 0

I have a list view which has been registered for a context menu. For some items within the list a context menu is not applicable. In these cases I just don’t inflate a menu in the onCreateContextMenu method.

Unfortunately this means that when items that don’t display a context menu are long-clicked Android then handles this as a short-click (presumably because the context menu would normally return true to say that the long-click event has been handled).

This results in an inconsistent behaviour in the listview – some items show a context menu when you long click them – others don’t and then perform the default click behaviour. How can I ensure that even items that don’t display a context menu consume the long click so that the onItemClick method isn’t called?

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

  AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
  Playable playable = (Playable) info.targetView.getTag(R.id.playable);
  if (playable != null && !(playable instanceof AutoRadioStation) && !(playable.getId().equals(Playlist.AUTOMATIC_PLAYLIST))) {
    v.setTag(R.id.playable, playable); // This copies the tag so that it is contained within the view used for the menu.
    Drawable stationImage = (Drawable) ((ImageView) info.targetView.findViewById(R.id.artwork)).getDrawable().getConstantState().newDrawable();

    menu.setHeaderTitle(playable.getName());
    menu.setHeaderIcon(stationImage);
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(R.menu.saved_context_menu, menu);
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T18:45:12+00:00Added an answer on June 8, 2026 at 6:45 pm

    I finally got around to implementing a version of NathanZ solution. There didn’t seem to be much stuff out there about turning a contextMenu into a DialogFragment so I’ll paste most of my solution here.

    Implementing an onLongItemClick listener also meant that I was able to have a long-click event that didn’t require a menu within the listview. Unfortunately because you can’t pass menus around to a dialog I had to reuse an existing ListViewElement type to store an id and a text string for each “menu” item in my listview.

      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int item, long position) {
    
        Playable playable = (Playable) view.getTag(R.id.playable);
        //Switch haptic feedback off by default so if we don't handle the long click we don't vibrate
        parent.setHapticFeedbackEnabled(false);
    
        if (playable == null) {
          // This must be a message bar so the only option is to update all saved content
          updateAll();
          parent.setHapticFeedbackEnabled(true);
        } else {
          if (!(playable instanceof AutoRadioStation) && !(playable.getId().equals(Playlist.AUTOMATIC_PLAYLIST))) {
            Drawable drawable = (Drawable) ((ImageView) view.findViewById(R.id.artwork)).getDrawable().getConstantState().newDrawable();
            showContextDialog(playable, drawable);
            parent.setHapticFeedbackEnabled(true);
          }
        }
        return true;
      }
      private void showContextDialog(Playable playable, Drawable drawable) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        final List<ListViewElement> array = new ArrayList<ListViewElement>();
        array.add(new ListViewElement(R.id.menu_share, null, getString(R.string.share), true));
        array.add(new ListViewElement(R.id.menu_delete, null, getString(R.string.delete), true));
        ContextMenuDialog dialog = new ContextMenuDialog(drawable, playable.getName(), array, playable);
        dialog.setOnItemClickListener(this);
        dialog.show(fm, "Context Menu");
      }
    
      //Callback from the ContextMenuDialog class
      @Override
      public void onItemClickDialogFragment(int option, Playable playable) {
        switch (option) {
          case R.id.menu_delete :
            // Perform delete actions
            break;
          case R.id.menu_share :
            // Perform share actions
            break;
        }
      }
    
    
    public class ContextMenuDialog extends DialogFragment implements OnItemClickListener {
    
      private Drawable drawableIcon;
      private String title;
      private List<ListViewElement> values;
      private Playable playable;
      private DialogFragmentOnItemClickListener listener;
    
      public interface DialogFragmentOnItemClickListener {
        void onItemClickDialogFragment(int option, Playable playable);
      }
    
      public void setOnItemClickListener(DialogFragmentOnItemClickListener listener) {
        this.listener = listener;
      }
    
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Create the dialog without a title since the layout includes a customized title
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.MyDialogStyle);
      }
    
      public ContextMenuDialog(Drawable drawableIcon, String title, List<ListViewElement> values, Playable playable) {
        this.drawableIcon = drawableIcon;
        this.title = title;
        this.values = values;
        this.playable = playable;
      }
    
    
      public ContextMenuDialog(int drawableResource, String title, List<ListViewElement> values, Playable playable) {
        this.drawableIcon = getResources().getDrawable(drawableResource);
        this.title = title;
        this.values = values;
        this.playable = playable;
      }
    
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.context_menu, container, true);
        TextView titleView = (TextView) view.findViewById(R.id.context_menu_title);
        titleView.setText(title);
        ImageView icon = (ImageView) view.findViewById(R.id.context_menu_artwork);
        icon.setImageDrawable(drawableIcon);
    
        ListView listView = (ListView) view.findViewById(R.id.context_menu_listview);
        ContextMenuAdapter adapter = new ContextMenuAdapter(getActivity(), R.layout.context_menu_list_item, values);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
        return view;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list view item which has a button and displays properties on
I have a basic iPhone/iPad app which has a ; a. List view &
I have a tab view which has a listview in one of these tabs.
I have a ListView, which can either have a context menu, or not. I
I have a silverlight control (View) which displays a list of items in a
I currently have a list view which has several rows of data and I
I have a list view which is bound to a DataTable source. The sorting
I have chat list view in which sender name should be left aligned and
I have a list view which is populated from a SQL database. I am
I have an asp.net view which has two partial views. One for edit information

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.