0

is there any easy way to select whole words from TextView by touching them? This functionality is in dictionary app ColorDict.

  1. Search for word "word"
  2. Select word "theorem" and it now appears in the search box so I can search it faster by clicking on search.

I want to be able to select those words. That ScrollView which is on top is misleading. It appears after selecting "Select a word" in dialog which appears after long press on something in TextView (you can see word "theorem" on the bottom - long press on "theorem").

Thank you for suggestions.

Dalton
  • 347
  • 3
  • 15

1 Answers1

1

Try setting this attribute to your TextView:

android:textIsSelectable="true"

EDIT:

private static final int MENU_ITEM_ID = 0x42;
private TextView targetTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    targetTextView = (TextView) findViewById(R.id.target_textview);
    targetTextView.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // you can remove the default menu items if you wish
            menu.removeItem(android.R.id.selectAll);
            menu.removeItem(android.R.id.cut);
            menu.removeItem(android.R.id.copy);
            return true;
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            /*
            * called when the action mode is created. Here you can
            * generate action buttons for this action mode.
            * */
            menu.add(0, MENU_ITEM_ID, 0, "Menu Item").setIcon(android.R.drawable.ic_dialog_alert);
            return true;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            // called when the action mode is about to be destroyed
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch (item.getItemId()) {
                case MENU_ITEM_ID:
                    int start = targetTextView.getSelectionStart();
                    int end = targetTextView.getSelectionEnd();
                    CharSequence selectedText = targetTextView.getText().subSequence(start, end);
                    Toast.makeText(MainActivity.this, selectedText, Toast.LENGTH_SHORT).show();
                    mode.finish();
                    return true;
                default:
                    break;
            }
            return false;
        }
    });
}
jvincek
  • 560
  • 4
  • 14
  • Okay, and how to make it select whole word after short press? How to trigger something after short press on the word? There is some handler? – Dalton Jul 24 '14 at 15:47
  • I've added sample code to enable this functionality. However, beware that this is available as of API level 11. I am unsure how to do this for earlier Android versions. – jvincek Jul 24 '14 at 16:04
  • Did this resolve your problem? If yes, please mark this answer as accepted. If not, please provide more information on the issues. – jvincek Jul 24 '14 at 16:28
  • Thank you for example and guiding me to setCustomSelectionActionModeCallback method. Didn't find this. After I googled this method I found http://stackoverflow.com/questions/22832123/get-selected-text-from-textview on SO and it seems very similar. I don't want to select words by this way, but right now rethought priorities. I will get back to this after some other problem I am facing right now. I want to extract text by simply Touch (with that touch automaticaly select whole word) -> (automaticaly) Put word to EditText. But thanks.. accepting on the way. – Dalton Jul 24 '14 at 17:07