Sushant Bhatnagar
Sushant Bhatnagar

Reputation: 3774

How to get selected text from edittext in android?

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.

Upvotes: 19

Views: 13290

Answers (3)

Induwara Rajapaksha
Induwara Rajapaksha

Reputation: 71

Kotlin

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

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

Upvotes: 2

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

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

String selectedText = et.getText().toString().substring(startSelection, endSelection);

Upvotes: 35

MAC
MAC

Reputation: 15847

getSelectionStart()
getSelectionEnd()

see this link

Upvotes: 0

Related Questions