2

I working on module to search grab text from edit text and search it into current text view. If present highlight this text in current text view. I also googled for this code but didn't found any relevant answer.

tv=(TextView) mView.findViewById(R.id.detailsText);
    edit_text=(EditText)findViewById(R.id.searchText);


edit_text.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            hightLightText(tv, s.toString());
        }
    });



}

void hightLightText(TextView textView, String searchString){
    try{

        String s=getResources().getString(R.string.firstpage);

        String withHighLightedText = s.replaceAll(searchString, "<font color='red'>"+searchString+"</font>");
        String styledText = "This is <font color='red'>simple</font>.";
        textView.setText(Html.fromHtml(withHighLightedText), TextView.BufferType.SPANNABLE);
        }catch(Exception ex){

        }

}
u_pendra
  • 908
  • 1
  • 10
  • 25

3 Answers3

2

HTML tag formatting within TextView is very limited.

<b>Bold</b> and <i>Italic</i>

do work, but

<font color>

sadly does not.

Use Spannable instead to highlight to portions of text you want.

Here is an example for making selected text Italic within an EditText View: Is there any example about Spanned and Spannable text

Community
  • 1
  • 1
David Manpearl
  • 12,362
  • 8
  • 55
  • 72
1

call the below function for highlighting.

public void hightLightText(TextView textView, String searchString) {
    String s = textView.getText().toString();
    SpannableString str = new SpannableString(s);
    if (searchString != null && !searchString.equalsIgnoreCase("")) {
        int startIndex = 0;
        while (true) {
            startIndex = s.indexOf(searchString, startIndex);

            if (startIndex >= 0) {
                str.setSpan(new BackgroundColorSpan(Color.YELLOW),
                        startIndex, startIndex + searchString.length(),
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                startIndex++;
            } else {
                break;
            }
        }
    }
    textView.setText(str);
}
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
0
EditText edit_text;
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edittest);
    tv=(TextView) findViewById(R.id.detailsText);
    edit_text=(EditText)findViewById(R.id.searchText);


  edit_text.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            hightLightText(tv, edit_text.getText().toString().trim());
        }
    });



}


void hightLightText(TextView textView, String searchString){
    try{

        String s=getResources().getString(R.string.app_name);

        String withHighLightedText = s.replaceAll(searchString, "<font color='red'>"+searchString+"</font>");
        String styledText = "This is <font color='red'>simple</font>.";
        textView.setText(Html.fromHtml(withHighLightedText), TextView.BufferType.SPANNABLE);
        }catch(Exception ex){

        }

}

In your code just change the bellow method

 @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        hightLightText(tv, edit_text.getText().toString().trim());
    }
Srikanth
  • 584
  • 1
  • 6
  • 21