How to get selected text from edittext in android?
Asked Answered
E

3

19

I have a edittext in which some text are selected . I want to get only selected text from edittext on click of button .

Please suggest me usable link or sample code.

Equivalency answered 9/4, 2012 at 8:33 Comment(2)
SearchMountie
Have a look at this answerGodown
P
35
EditText et=(EditText)findViewById(R.id.edit);

int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();

String selectedText = et.getText().toString().substring(startSelection, endSelection);
Panteutonism answered 9/4, 2012 at 8:37 Comment(1)
after it if u want to check whether selectedtext is empty or not. you must use isEmpty function .Equivalency
V
2

Kotlin

val startSelection: Int = editText.selectionStart
val endSelection: Int = editText.selectionEnd

val selectedText: String = editText.text.substring(startSelection, endSelection)
Vouch answered 15/5, 2021 at 5:13 Comment(0)
B
0
getSelectionStart()
getSelectionEnd()

see this link

Backfield answered 9/4, 2012 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.