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.

Sushant Bhatnagar
  • 3,474
  • 10
  • 30
  • 42

3 Answers3

34
EditText et=(EditText)findViewById(R.id.edit);

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

String selectedText = et.getText().toString().substring(startSelection, endSelection);
Mirco Widmer
  • 2,081
  • 1
  • 19
  • 44
Mohammed Azharuddin Shaikh
  • 40,812
  • 14
  • 95
  • 115
1

Kotlin

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

val selectedText: String = editText.text.substring(startSelection, endSelection)
0
getSelectionStart()
getSelectionEnd()

see this link

MAC
  • 15,659
  • 8
  • 53
  • 95