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.

3 Answers3

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>
Umit Kaya
  • 5,771
  • 3
  • 38
  • 52
  • No I don't want the whole text to be highlighted, but will let the user to just chose which text to highlight –  Aug 18 '14 at 06:42
  • then you need to use textIsSelectable specifically. – Umit Kaya Aug 18 '14 at 06:43
  • 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 –  Aug 18 '14 at 06:47
  • 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. – Umit Kaya Aug 18 '14 at 06:51
  • No, i didn't. I can't get the selected text and highlight with a different colour –  Aug 18 '14 at 09:20
1

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

fcdt
  • 2,371
  • 5
  • 14
  • 26
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

Community
  • 1
  • 1
Raviindra
  • 114
  • 1
  • 7
  • alright thanks but I want to highlight the text with other color. How can I do that? –  Aug 18 '14 at 06:34
  • [link](http://stackoverflow.com/questions/2120035/highlight-text-in-textview-or-webview) – Raviindra Aug 18 '14 at 06:38