How to Highlight Multiple Words in Edittext

How to highlight Multiple words in EditText?

I have wrote a simple method that allow you pass TextView (or child classes Button, Edittext etc.).

1. If you want to highlight a text like in find word in paragraph. You can use below method like.

setHighLightedText(yourTextView_Edittext_Button, "a");

Which gives you result like this.

Sample Image

    /**
* use this method to highlight a text in TextView
* @param tv TextView or Edittext or Button or child of TextView class
* @param textToHighlight Text to highlight
*/
public void setHighLightedText(TextView tv, String textToHighlight) {
String tvt = tv.getText().toString();
int ofe = tvt.indexOf(textToHighlight, 0);
Spannable wordToSpan = new SpannableString(tv.getText());
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(textToHighlight, ofs);
if (ofe == -1)
break;
else {
wordToSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), ofe, ofe + textToHighlight.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
}
}
}

2. If you want make clickable highlighted text (like click on terms & condition text) then use this code as below:

 setClickableHighLightedText(yourTextView_Edittext_Button, "go to settings", new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: do your stuff here
}
});

Which gives you result like

Sample Image

/**
* use this method to set clickable highlighted a text in TextView
*
* @param tv TextView or Edittext or Button or child of TextView class
* @param textToHighlight Text to highlight
*/
public void setClickableHighLightedText(TextView tv, String textToHighlight, View.OnClickListener onClickListener) {
String tvt = tv.getText().toString();
int ofe = tvt.indexOf(textToHighlight, 0);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
if (onClickListener != null) onClickListener.onClick(textView);
}

@Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(0xff0000ff);
ds.setUnderlineText(true);
}
};
SpannableString wordToSpan = new SpannableString(tv.getText());
for (int ofs = 0; ofs < tvt.length() && ofe != -1; ofs = ofe + 1) {
ofe = tvt.indexOf(textToHighlight, ofs);
if (ofe == -1)
break;
else {
wordToSpan.setSpan(clickableSpan, ofe, ofe + textToHighlight.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(wordToSpan, TextView.BufferType.SPANNABLE);
tv.setMovementMethod(LinkMovementMethod.getInstance());
}
}
}

This is a workaround, you can customise spans according to your need. Some good tutorials Android text styles and one other

Highlight text in multiple EditText controls simultaneously

I know, but its (as far as I know?) the only way to mark text in an EditText control.

EditText supports Spannable objects, so you can apply highlights to text (e.g., background colors) yourself.

This sample project demonstrates a search field that applies a background color to a larger piece of text based upon the search results. The key part is the searchFor() method:

  private void searchFor(String text) {
TextView prose=(TextView)findViewById(R.id.prose);
Spannable raw=new SpannableString(prose.getText());
BackgroundColorSpan[] spans=raw.getSpans(0,
raw.length(),
BackgroundColorSpan.class);

for (BackgroundColorSpan span : spans) {
raw.removeSpan(span);
}

int index=TextUtils.indexOf(raw, text);

while (index >= 0) {
raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
+ text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index=TextUtils.indexOf(raw, text, index + text.length());
}

prose.setText(raw);
}

Note, though, that your "output string" probably should be a TextView, not an EditText. EditText is for input, not output.

Highlight All Words that is searched via EditText

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:

Sample Image



Related Topics



Leave a reply



Submit