Home > Net >  Android get focused word from edittext
Android get focused word from edittext

Time:12-19

Lets say I have an Android edittext with sentence: "I like Stackoverflow very much". Then I use my keyboard to be focused on the word Stackoverflow and now I click the button in my app that adds / removes for example bold effect for this specific word.

Is it possible to do that?

Thank you in advance.

CodePudding user response:

if you are talking about position of cursor then use

int cursorPosition = et.getSelectionStart();

after taking position of cursor you should take whole text and apply desired effect. for example split text by white characters (spaces, enters), calculate on which word cursor is pointing and add/remove bold spannable

note that et.getText() returns Editable, not just usual String. So you can do some work on this class, e.g. check if word with cursor inside is already bold or other spannable applied

  • Related