Example usage for android.view ContextMenu hasVisibleItems

List of usage examples for android.view ContextMenu hasVisibleItems

Introduction

In this page you can find the example usage for android.view ContextMenu hasVisibleItems.

Prototype

public boolean hasVisibleItems();

Source Link

Document

Return whether the menu currently has item items that are visible.

Usage

From source file:com.todoroo.astrid.core.CustomFilterActivity.java

private void setUpListeners() {
    ((Button) findViewById(R.id.add)).setOnClickListener(new View.OnClickListener() {
        @Override//from  w  w w  . jav  a2s  .  c  om
        public void onClick(View v) {
            listView.showContextMenu();
        }
    });

    final Button saveAndView = ((Button) findViewById(R.id.saveAndView));
    saveAndView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveAndView();
        }
    });

    filterName.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            if (s.length() == 0) {
                saveAndView.setText(R.string.CFA_button_view);
                saveAndView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.tango_next, 0);
            } else {
                saveAndView.setText(R.string.CFA_button_save);
                saveAndView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.tango_save, 0);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //
        }
    });

    listView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
            if (menu.hasVisibleItems()) {
                /* If it has items already, then the user did not click on the "Add Criteria" button, but instead
                   long held on a row in the list view, which caused CustomFilterAdapter.onCreateContextMenu
                   to be invoked before this onCreateContextMenu method was invoked.
                 */
                return;
            }

            int i = 0;
            for (CustomFilterCriterion item : criteria.values()) {
                try {
                    menu.add(CustomFilterActivity.MENU_GROUP_FILTER, i, 0, item.name);
                } catch (NullPointerException e) {
                    throw new NullPointerException("One of the criteria is null. Criteria: " + criteria); //$NON-NLS-1$
                }
                i++;
            }
        }
    });
}