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,684
  • 10
  • 31
  • 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,139
  • 1
  • 20
  • 44
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 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,799
  • 8
  • 54
  • 95