Code example for ContextMenu

Methods: setHeaderTitlesetHeaderViewaddaddSubMenu

0
         
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
         
        String text = ((TextView) info.targetView).getText().toString();
        if (text.contains("[0]")) {
            menu.setHeaderTitle("This is a test of the title and the icon").setHeaderIcon(
                    android.R.drawable.sym_def_app_icon);
        } else if (text.contains("[1]")) {
            menu.setHeaderTitle("This is a test of just the title");
        } else { 
            TextView textView = new TextView(this);
            textView.setText("This is a test of a custom View");
            menu.setHeaderView(textView);
        } 
         
        menu.add(0, 0, 0, "Test 1");
        SubMenu sm = menu.addSubMenu(0, 0, 0, "Test 1.5 SM");
        sm.add(0, 0, 0, "CM Subitem 1");
        sm.add(0, 0, 0, "CM Subitem 2");
        sm.add(0, 0, 0, "CM Subitem 3");
        menu.add(0, 0, 0, "Test 2");
        menu.add(0, 0, 0, "Test 3");
Connect your IDE to all the code out there  Get Codota for Java