2

I would like to do an app which has an EditText or TextView that can be selected upon click and highlight the selected text. How can I do that? I tried overriding onClick method on my EditText but seems not working.

Here's what I've tried so far:

etx.setOnLongClickListener(new OnLongClickListener() { @Override public boolean onLongClick(View v) { int startSelection = etx.getSelectionStart(); int endSelection = etx.getSelectionEnd(); //String selectedText = etx.getText().toString().substring(startSelection, endSelection); Spannable spannable=new SpannableString(etx.getText().toString()); spannable.setSpan(new ForegroundColorSpan(Color.BLUE), startSelection, endSelection, 0); etx.setText(spannable); return true; } }); <EditText android:id="@+id/tvOrdinanceTitle" android:layout_width="wrap_content" android:textColor="@android:color/black" android:cursorVisible="false" android:layout_height="wrap_content" android:background="#00000000" > </EditText> 

But it's not working. Any workaround? I would gladly appreciate your help. Thanks.

2
  • try this stackoverflow.com/a/16123610/3020568 Commented Aug 18, 2014 at 6:22
  • @deniz thanks. How can I let the user change the highlight color of the text? Commented Aug 18, 2014 at 6:34

3 Answers 3

2

You can include in your .xml:

android:selectAllOnFocus="true" 

Specifically:

android:textIsSelectable=Indicates that the content of a non-editable text can be selected. android:selectAllOnFocus=If the text is selectable, select it all when the view takes. focus. 

Or programmatically:

etx.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus) { etx.setSelection(editText.getText().toString().length()); } } }); 

And to identify what colors to be used. Lets say this is your editText:

 <EditText android:id="@+id/tvOrdinanceTitle" android:layout_width="wrap_content" android:cursorVisible="false" android:layout_height="wrap_content" android:background="@color/etxt_color" > </EditText> 

Then create res/color/etxt_color.xml

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:color="#000000" /> <!-- pressed --> <item android:state_focused="true" android:color="#000000" /> <!-- focused --> <item android:color="#FFFFFF" /> <!-- default --> </selector> 
Sign up to request clarification or add additional context in comments.

5 Comments

No I don't want the whole text to be highlighted, but will let the user to just chose which text to highlight
then you need to use textIsSelectable specifically.
yes I've done that with android:textIsSelectable but my question how do you get the highlighted text and let the user choose what the highlight colour they want
you can play around with the colors, i explained in my updated answer. This is the basic way, how you want to let user choose is another programmatic trick if you really want, you can relate to this.
No, i didn't. I can't get the selected text and highlight with a different colour
2

editText.selectAll() is the function that worked for me.

Comments

0

you can better use a TextView with a background set like and EditText ,

since using on click listener on EditText is not a good practice because it shows the keyboard when you click on it, but still if you want to use EditText for the same purpose , you can refer this: Android EditText onClickListener

2 Comments

alright thanks but I want to highlight the text with other color. How can I do that?