Example usage for android.view ContextMenu add

List of usage examples for android.view ContextMenu add

Introduction

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

Prototype

public MenuItem add(CharSequence title);

Source Link

Document

Add a new item to the menu.

Usage

From source file:com.hughes.android.dictionary.DictionaryActivity.java

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
    final RowBase row = (RowBase) getListAdapter().getItem(adapterContextMenuInfo.position);

    final android.view.MenuItem addToWordlist = menu.add(getString(R.string.addToWordList, wordList.getName()));
    addToWordlist.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(android.view.MenuItem item) {
            onAppendToWordList(row);//  ww w  .  jav a2s. c o m
            return false;
        }
    });

    final android.view.MenuItem share = menu.add("Share");
    share.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(android.view.MenuItem item) {
            Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, row.getTokenRow(true).getToken());
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, row.getRawText(saveOnlyFirstSubentry));
            startActivity(shareIntent);
            return false;
        }
    });

    final android.view.MenuItem copy = menu.add(android.R.string.copy);
    copy.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(android.view.MenuItem item) {
            onCopy(row);
            return false;
        }
    });

    if (selectedSpannableText != null) {
        final String selectedText = selectedSpannableText;
        final android.view.MenuItem searchForSelection = menu
                .add(getString(R.string.searchForSelection, selectedSpannableText));
        searchForSelection.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(android.view.MenuItem item) {
                jumpToTextFromHyperLink(selectedText, selectedSpannableIndex);
                return false;
            }
        });
        // Rats, this won't be shown:
        //searchForSelection.setIcon(R.drawable.abs__ic_search);
    }

    if ((row instanceof TokenRow || selectedSpannableText != null) && ttsReady) {
        final android.view.MenuItem speak = menu.add(R.string.speak);
        final String textToSpeak = row instanceof TokenRow ? ((TokenRow) row).getToken()
                : selectedSpannableText;
        updateTTSLanguage(row instanceof TokenRow ? indexIndex : selectedSpannableIndex);
        speak.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem item) {
                textToSpeech.speak(textToSpeak, TextToSpeech.QUEUE_FLUSH, new HashMap<String, String>());
                return false;
            }
        });
    }
}

From source file:com.money.manager.ex.home.HomeFragment.java

/**
 * Context menu for account entries.//from w  ww. j av  a  2s  .  c om
 */
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    if (!(v instanceof ExpandableListView))
        return;

    ExpandableListView.ExpandableListContextMenuInfo info = (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
    int type = ExpandableListView.getPackedPositionType(info.packedPosition);
    int groupPosition = ExpandableListView.getPackedPositionGroup(info.packedPosition);
    int childPosition = ExpandableListView.getPackedPositionChild(info.packedPosition);

    // ignore long-press on group items.
    if (type != ExpandableListView.PACKED_POSITION_TYPE_CHILD)
        return;

    // get adapter.
    HomeAccountsExpandableAdapter accountsAdapter = (HomeAccountsExpandableAdapter) mExpandableListView
            .getExpandableListAdapter();
    Object childItem = accountsAdapter.getChild(groupPosition, childPosition);
    QueryAccountBills account = (QueryAccountBills) childItem;

    //        menu.setHeaderIcon(android.R.drawable.ic_menu_manage);
    menu.setHeaderTitle(account.getAccountName());
    String[] menuItems = getResources().getStringArray(R.array.context_menu_account_dashboard);
    for (String menuItem : menuItems) {
        menu.add(menuItem);
    }

    // balance account should work only for transaction accounts.
    AccountService service = new AccountService(getActivity());
    List<String> accountTypes = service.getTransactionAccountTypeNames();
    String accountType = account.getAccountType();
    if (accountTypes.contains(accountType)) {
        menu.add(R.string.balance_account);
    }

    // Investment menu items.
    if (accountType.equals(AccountTypes.INVESTMENT.toString())) {
        menu.add(Menu.NONE, ContextMenuIds.Portfolio.getId(), 0, getString(R.string.portfolio));
    }
}

From source file:uk.co.pjmobile.mobile_apps.page_turner_reader.ReadingActivity.java

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

    // This is a hack to give the longclick handler time
    // to find the word the user long clicked on.

    if (this.selectedWord != null) {

        final CharSequence word = this.selectedWord;

        String header = String.format(getString(R.string.word_select), selectedWord);
        menu.setHeaderTitle(header);//  www  .  j  av a 2s  .  co  m

        final Intent intent = new Intent(PICK_RESULT_ACTION);
        intent.putExtra(EXTRA_QUERY, word.toString()); // Search Query
        intent.putExtra(EXTRA_FULLSCREEN, false); //
        intent.putExtra(EXTRA_HEIGHT, 400); // 400pixel, if you don't
        // specify, fill_parent"
        intent.putExtra(EXTRA_GRAVITY, Gravity.BOTTOM);
        intent.putExtra(EXTRA_MARGIN_LEFT, 100);

        if (isIntentAvailable(this, intent)) {
            MenuItem item = menu.add(getString(R.string.dictionary_lookup));
            item.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    startActivityForResult(intent, 5);
                    return true;
                }
            });
        }

        MenuItem newItem = menu.add(getString(R.string.wikipedia_lookup));
        newItem.setOnMenuItemClickListener(new BrowserSearchMenuItem(
                "http://en.wikipedia.org/wiki/Special:Search?search=" + URLEncoder.encode(word.toString())));

        MenuItem newItem2 = menu.add(getString(R.string.google_lookup));
        newItem2.setOnMenuItemClickListener(new BrowserSearchMenuItem(
                "http://www.google.com/search?q=" + URLEncoder.encode(word.toString())));

        this.selectedWord = null;
    }
}

From source file:net.nightwhistler.pageturner.fragment.ReadingFragment.java

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

    // This is a hack to give the longclick handler time
    // to find the word the user long clicked on.

    if (this.selectedWord != null) {

        final CharSequence word = this.selectedWord.getText();
        final int startIndex = this.selectedWord.getStartOffset();
        final int endIndex = this.selectedWord.getEndOffset();

        String header = String.format(getString(R.string.word_select), selectedWord.getText());

        menu.setHeaderTitle(header);//  ww  w. j a va  2  s . c  o m

        if (isDictionaryAvailable()) {
            android.view.MenuItem item = menu.add(getString(R.string.dictionary_lookup));
            onMenuPress(item).thenDo(() -> lookupDictionary(word.toString()));
        }

        menu.add(R.string.highlight).setOnMenuItemClickListener(item -> {
            highLight(startIndex, endIndex, word.toString());
            return false;
        });

        android.view.MenuItem lookUpWikipediaItem = menu.add(getString(R.string.wikipedia_lookup));

        onMenuPress(lookUpWikipediaItem).thenDo(() -> lookupWikipedia(word.toString()));

        android.view.MenuItem lookUpWiktionaryItem = menu.add(getString(R.string.lookup_wiktionary));

        lookUpWiktionaryItem.setOnMenuItemClickListener(item -> {
            lookupWiktionary(word.toString());
            return true;
        });

        android.view.MenuItem lookupGoogleItem = menu.add(getString(R.string.google_lookup));

        lookupGoogleItem.setOnMenuItemClickListener(item -> {
            lookupGoogle(word.toString());
            return true;
        });

        this.selectedWord = null;
    }
}

From source file:net.nightwhistler.pageturner.activity.ReadingFragment.java

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

    // This is a hack to give the longclick handler time
    // to find the word the user long clicked on.

    if (this.selectedWord != null) {

        final CharSequence word = this.selectedWord.getText();
        final int startIndex = this.selectedWord.getStartOffset();
        final int endIndex = this.selectedWord.getEndOffset();

        String header = String.format(getString(R.string.word_select), selectedWord.getText());

        menu.setHeaderTitle(header);/*w w w  .  j  av  a  2 s .  c om*/

        if (isDictionaryAvailable()) {
            android.view.MenuItem item = menu.add(getString(R.string.dictionary_lookup));
            item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(android.view.MenuItem item) {
                    lookupDictionary(word.toString());
                    return true;
                }
            });
        }

        menu.add(R.string.highlight).setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem menuItem) {

                highLight(startIndex, endIndex, word.toString());

                return false;
            }
        });

        android.view.MenuItem newItem = menu.add(getString(R.string.wikipedia_lookup));
        newItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem item) {
                lookupWikipedia(word.toString());
                return true;
            }
        });

        android.view.MenuItem newItem3 = menu.add(getString(R.string.lookup_wiktionary));
        newItem3.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem item) {
                lookupWiktionary(word.toString());
                return true;
            }
        });

        android.view.MenuItem newItem2 = menu.add(getString(R.string.google_lookup));
        newItem2.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem item) {
                lookupGoogle(word.toString());
                return true;
            }
        });

        this.selectedWord = null;
    }
}

From source file:net.zorgblub.typhon.fragment.ReadingFragment.java

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

    // This is a hack to give the longclick handler time
    // to find the word the user long clicked on.

    if (this.selectedWord != null && !this.config.isRikaiEnabled()) {

        final CharSequence word = this.selectedWord.getText();
        final int startIndex = this.selectedWord.getStartOffset();
        final int endIndex = this.selectedWord.getEndOffset();

        String header = String.format(getString(R.string.word_select), selectedWord.getText());

        menu.setHeaderTitle(header);//from w w w .j  a  va2 s. co m

        if (isDictionaryAvailable()) {
            android.view.MenuItem item = menu.add(getString(R.string.dictionary_lookup));
            onMenuPress(item).thenDo(() -> lookupDictionary(word.toString()));
        }

        menu.add(R.string.highlight).setOnMenuItemClickListener(item -> {
            highLight(startIndex, endIndex, word.toString());
            return false;
        });

        android.view.MenuItem lookUpWikipediaItem = menu.add(getString(R.string.wikipedia_lookup));

        onMenuPress(lookUpWikipediaItem).thenDo(() -> lookupWikipedia(word.toString()));

        android.view.MenuItem lookUpWiktionaryItem = menu.add(getString(R.string.lookup_wiktionary));

        lookUpWiktionaryItem.setOnMenuItemClickListener(item -> {
            lookupWiktionary(word.toString());
            return true;
        });

        android.view.MenuItem lookupGoogleItem = menu.add(getString(R.string.google_lookup));

        lookupGoogleItem.setOnMenuItemClickListener(item -> {
            lookupGoogle(word.toString());
            return true;
        });

        this.selectedWord = null;
    }
}

From source file:com.aujur.ebookreader.activity.ReadingFragment.java

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

    // This is a hack to give the longclick handler time
    // to find the word the user long clicked on.

    if (this.selectedWord != null) {

        final CharSequence word = this.selectedWord.getText();
        final int startIndex = this.selectedWord.getStartOffset();
        final int endIndex = this.selectedWord.getEndOffset();

        String header = String.format(getString(R.string.word_select), selectedWord.getText());

        menu.setHeaderTitle(header);//from   w w w .j  a va  2  s.c o  m

        if (isDictionaryAvailable()) {
            android.view.MenuItem item = menu.add(getString(R.string.dictionary_lookup));
            item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(android.view.MenuItem item) {
                    lookupDictionary(word.toString());
                    return true;
                }
            });
        }

        menu.add(R.string.highlight).setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem menuItem) {

                highLight(startIndex, endIndex, word.toString());

                return false;
            }
        });

        android.view.MenuItem newItem = menu.add(getString(R.string.wikipedia_lookup));
        newItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem item) {
                lookupWikipedia(word.toString());
                return true;
            }
        });

        android.view.MenuItem newItem3 = menu.add(getString(R.string.lookup_wiktionary));
        newItem3.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem item) {
                lookupWiktionary(word.toString());
                return true;
            }
        });

        android.view.MenuItem newItem2 = menu.add(getString(R.string.google_lookup));
        newItem2.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem item) {
                lookupGoogle(word.toString());
                return true;
            }
        });

        android.view.MenuItem newItem4 = menu.add(getString(R.string.direitonet_lookup));
        newItem4.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(android.view.MenuItem item) {
                lookupLawDictionary(word.toString());
                return true;
            }
        });

        this.selectedWord = null;
    }
}

From source file:org.tigase.mobile.roster.RosterFragment.java

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

    final Long id = extractId(menuInfo);

    if (id != null && id != -1) {
        RosterItem r = getJid(id);//from w ww .ja v a2 s  . c  o  m
        final boolean sessionEstablished = r != null && isSessionEstablished(r.getSessionObject());

        MenuInflater m = new MenuInflater(getActivity());
        try {
            Presence p = r.getSessionObject().getPresence().getBestPresence(r.getJid());
            if (p != null && p.getType() == null) {
                SubMenu sm = menu.addSubMenu(R.id.contactsOnlineGroup, Menu.NONE, Menu.NONE, "Chat with");
                prepareResources(sm, id);
            }
        } catch (Exception e) {
        }
        m.inflate(R.menu.roster_context_menu, menu);

        JaxmppCore jaxmpp = this.getMulti().get(r.getSessionObject());
        if (jaxmpp != null && sessionEstablished) {
            GeolocationModule module = jaxmpp.getModule(GeolocationModule.class);
            if (module != null) {
                ContentValues location = module.getLocationForJid(r.getJid());
                if (location != null) {
                    Double lat = location.getAsDouble(GeolocationTableMetaData.FIELD_LAT);
                    Double lon = location.getAsDouble(GeolocationTableMetaData.FIELD_LON);
                    String uriStr = null;
                    if (lon == null || lat == null) {
                        String str = "";
                        String val = location.getAsString(GeolocationTableMetaData.FIELD_STREET);
                        Log.v(TAG, "Street = " + String.valueOf(val));
                        if (val != null) {
                            str += val;
                        }
                        val = location.getAsString(GeolocationTableMetaData.FIELD_LOCALITY);
                        Log.v(TAG, "Locality = " + String.valueOf(val));
                        if (val != null) {
                            if (!isEmpty(str)) {
                                str += " ";
                            }
                            str += val;
                        }
                        val = location.getAsString(GeolocationTableMetaData.FIELD_COUNTRY);
                        Log.v(TAG, "Country = " + String.valueOf(val));
                        if (val != null) {
                            if (!isEmpty(str)) {
                                str += " ";
                            }
                            str += val;
                        }

                        if (!isEmpty(str)) {
                            str = str.replace(' ', '+');
                            uriStr = "geo:0,0?q=" + str;
                        }
                    } else {
                        Log.v(TAG, "latitude = " + String.valueOf(lat));
                        Log.v(TAG, "longitude = " + String.valueOf(lon));
                        uriStr = "geo:" + String.valueOf(lat) + "," + String.valueOf(lon) + "?z=14";
                    }
                    if (uriStr != null) {
                        uriStr += "(" + RosterDisplayTools.getDisplayName(r) + ")";
                        Log.v(TAG, "created geolocation uri = " + uriStr);
                        Uri uri = Uri.parse(uriStr);
                        MenuItem item = menu.add(R.string.geolocation_show);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        item.setIntent(intent);
                    }
                }
            }
        }

        menu.setGroupVisible(R.id.contactsOnlineGroup, sessionEstablished);
    }
}