11

Hello I want to know how to highlight all Words that is inputted in the EditText and will appear in the TextView this post is related to this one Highlight Textview Using EditText

Community
  • 1
  • 1
Mordiggian
  • 265
  • 1
  • 3
  • 11

2 Answers2

22

Say et is your EditText and tv is TextView object. Use the following code:


public class MotivationalQuotesActivity extends Activity {
    /** Called when the activity is first created. */

Button next;
EditText et; 
TextView tv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
   et = (EditText) findViewById(R.id.et);
   tv = (TextView) findViewById(R.id.tv);
   tv.setText("The name of our country is Bangladesh. Bangladesh is a land of rivers.");

   next = (Button) findViewById(R.id.button1);
    next.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                tv.setText("The name of our country is Bangladesh. Bangladesh is a land of rivers.");
                String ett =et.getText().toString();
                String tvt =tv.getText().toString();

                int ofe = tvt.indexOf(ett,0);   
                Spannable WordtoSpan = new SpannableString( tv.getText() );

        for(int ofs=0;ofs<tvt.length() && ofe!=-1;ofs=ofe+1)
        {       


              ofe = tvt.indexOf(ett,ofs);   
                  if(ofe == -1)
                      break;
                  else
                      {                       

                      WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                      tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
                      }


        }  


            }
        });

    }

}

The result is:

enter image description here

Imran Rana
  • 11,899
  • 7
  • 45
  • 51
  • i have 1 more question, how to erase or reset the highlight in the word? – Mordiggian May 31 '12 at 04:32
  • 1
    If you want to erase the highlight you can use 2 approaches: **First:** Set the raw text to the TextView like `tv.setText("The name of our country is Bangladesh. Bangladesh is a land of rivers.");` **Second:** Use above method of highlighting but use the color of the background(say **black** in this case) as the color of highlighting like: `WordtoSpan.setSpan(new BackgroundColorSpan(color.black), ofe, ofe+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);` – Imran Rana May 31 '12 at 04:50
  • sorry to bother you again but is it possible to highlight the words that is searched even if the TextView is not the same as the one in EditText like for exaple the textview has "The name of our country is Bangladesh. Bangladesh is a land of rivers." and I input "bangladesh" only. – Mordiggian May 31 '12 at 07:52
  • 1
    Yes for a case insensitive search, use toUpperCase or toLowerCase on both the original string and the substring before the indexOf :) – Imran Rana May 31 '12 at 08:02
0

One easy and quick way of highlighting a text, is to use string replace method. Replace the desire string with font tag

Spanned strHTML= Html.fromHtml("<center>"+full_string.replaceAll(strSrch,"<font color='yellow'>"+strSrch+"</font>")+"</center><br/>");
TextView tv=(TextView) v.findViewById(R.id.txtPager);
tv.setText(strHTML,TextView.BufferType.SPANNABLE);
sheldonzy
  • 5,505
  • 9
  • 48
  • 86
jafery
  • 1