0

I have an EditText and when i tap anywhere in the middle of a word, i want to be able to extract that word.

I know that i have to use getSelectionStart() and getSelectionEnd() methods, but i cannot make it work.

Can someone help me?

This is my code so far, on my onTextChanged method:

int cursorPosition = editMain.getSelectionStart();

        editMain.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                String selectedText = editMain
                        .getText()
                        .toString()
                        .substring(editMain.getSelectionStart(),
                                editMain.getSelectionEnd());

                buttonPopupHashtag.setText(selectedText);
                return false;

            }
        });

The problem is that i need to tap twice to get the word and i want it with a single tap.

pindleskin
  • 185
  • 1
  • 3
  • 14

1 Answers1

0

Maybe you will want to look at the next link. User already asked for it and there is a piece of code provided there doing what you requested: select a word on a tap in TextView/EditText

Community
  • 1
  • 1
Pavlos
  • 2,183
  • 2
  • 20
  • 27