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.