4

Im currently making a search engine like application for android and i want to highlight the searched word from edittext to textview... this is that i got so far and it only highlights the first word in the textview

TV.setText("Hello World", TextView.BufferType.SPANNABLE);
            Spannable WordtoSpan = (Spannable) TV.getText();
            WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), 0, notes.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            TV.setText(WordtoSpan);
Padma Kumar
  • 19,893
  • 17
  • 73
  • 130
Mordiggian
  • 265
  • 1
  • 3
  • 11

2 Answers2

6

I think you want to highlight a specific word of TextView which user types in a EditText. Say et is your EditText and tv is TextView object. Use the following code:


    String ett =et.getText().toString();
    String tvt =tv.getText().toString();

                int index = tvt.indexOf(ett);

                Spannable WordtoSpan = new SpannableString( tv.getText() );
                if(index != -1)
                {
                WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), index, index+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
                }
                else
                tv.setText("The name of our country is Bangladesh");

Here is the outcome:

enter image description here


Here is the complete 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");

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

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    String ett =et.getText().toString();
                    String tvt =tv.getText().toString();

                    int index = tvt.indexOf(ett);

                    Spannable WordtoSpan = new SpannableString( tv.getText() );
                    if(index != -1)
                    {
                    WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), index, index+ett.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tv.setText(WordtoSpan, TextView.BufferType.SPANNABLE);
                    }
                    else
                    tv.setText("The name of our country is Bangladesh");


                }
            });

        }

    }
Imran Rana
  • 11,899
  • 7
  • 45
  • 51
  • does this work even if there is 2 or more same words that is typed? – Mordiggian May 29 '12 at 01:31
  • THANK YOU VERY MUCH for this.. but if you know how to highlight all the same words that is typed in the edittext that would help me alot :D THANK YOU AGAIN FOR THIS – Mordiggian May 29 '12 at 01:47
  • sorry i already tried upvoting it earlier but it always says 15 or more reputation is required... my reputation just gone up this moments so now i can upvote your answer.. sorry again if my comment looks like im not accepting your answer.. – Mordiggian May 29 '12 at 12:56
  • http://stackoverflow.com/questions/10799732/highlight-all-words-that-is-searched-via-edittext this is the separate question that is related to this post... – Mordiggian May 29 '12 at 13:02
1

it could help

    TextView tv = (TextView) findViewById(R.id.hello);
    SpannableString s = new SpannableString(getResources().getString(R.string.linkify));

    Pattern p = Pattern.compile("abc");


     Matcher m = p.matcher(s);

    while (m.find()) {
        int start = m.start();
        int end = m.end();
        s.setSpan(new ForegroundColorSpan(Color.RED), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    tv.setText(s);
Dio_V
  • 69
  • 1
  • 10
  • Hi, welcome to SO. Instead of just dumping code, you should explain your train of thought to help users understand what you're doing. Thanks. – Cthulhu Mar 23 '16 at 11:50