Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 557
Sushant Bhatnagar Avatar asked Apr 09 '12 08:04

Sushant Bhatnagar


People also ask

How do I get text on android?

We have used getText() method to get the text entered in the EditText views. But getText() method returns an Editable instance and therefore we have typecasted it to convert it into String for further use. This can be done by using the toString() method.

How to Make text selectable in android studio?

To enable text in TextView for selection, set android:textIsSelectable attribute to “true” for the TextView widget in layout XML file.

What is TextView and EditText?

TextView is the widget used when you want the user to View the Text (such as a label, etc) and EditText used when you want the user to be able to edit the text. The text in either widget can be set programmatically or via xml using the android:text parameter.


1 Answers

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

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

String selectedText = et.getText().toString().substring(startSelection, endSelection);
like image 177
Mohammed Azharuddin Shaikh Avatar answered Oct 02 '22 02:10

Mohammed Azharuddin Shaikh