Change text Background color of a specific word in TextView

I want to change the background color of a specific word in a TextView :

As picture below

enter image description here

+7
source share
4 answers

Here is my working code that you can use to highlight some part of the line:

  private void highlightTextPart(TextView textView, int index, String regularExpression) { String fullText = textView.getText().toString(); int startPos = 0; int endPos = fullText.length(); String[] textParts = fullText.split(regularExpression); if (index < 0 || index > textParts.length - 1) { return; } if (textParts.length > 1) { startPos = fullText.indexOf(textParts[index]); endPos = fullText.indexOf(regularExpression, startPos); if (endPos == -1) { endPos = fullText.length(); } } Spannable spannable = new SpannableString(fullText); ColorStateList blueColor = new ColorStateList(new int[][] { new int[] {}}, new int[] { Color.BLUE }); TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan(null, Typeface.BOLD_ITALIC, -1, blueColor, null); BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.GREEN); spannable.setSpan(textAppearanceSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannable.setSpan(backgroundColorSpan, startPos, endPos, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannable); } 

Then in your activity call:

  int index = 3; String regularExpression = " "; String text = "Hello Qaru From BNK!"; TextView textView = (TextView) findViewById(R.id.textView); if (textView != null) { textView.setText(text); highlightTextPart(textView, index, regularExpression); } 

enter image description here

+6
source

try it

 String str = "Some really long string goes in here"; Spannable spannable = new SpannableString(str); spannable.setSpan(new BackgroundColorSpan( getResources().getColor(android.R.color.black) ), 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); yourTextView.setText(spannable); 

see link

+2
source
  String string = "As Soon As"; SpannableString spannableString = new SpannableString(string); toolBarSpan.setSpan(new BackgroundColorSpan(Color.parseColor("#ff0000")), 3, 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); yourTextView.setText(toolBarSpan); 

What is spannable and how does it work? Attach the specified markup object to the end of the range ... at the end of the text, or move the object to this range if it was already attached elsewhere. For more details, please, you can check the setSpan Spannable , and also know how this starting and ending number is required (for example, in anser 3 and 7 in BackgroundColorSpan

UPDATE

Use this working code.

 String mainString = "As Soon As"; String subString = "Soon"; if(mainString.contains(subString)) { int startIndex = mainString.indexOf(subString); int endIndex = startIndex + subString.length(); SpannableString spannableString = new SpannableString(mainString); spannableString.setSpan(new BackgroundColorSpan(Color.parseColor("#ff0000")), startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannableString); } 
+2
source

The easiest and best way to change the background color of a specific word in Android text view

enter image description here

 TextView txtMessage=findViewById(R.id.textview_msg); String text="Hello World!"; String search_term="Hello"; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { String newString = text.replaceAll(search_term, "<span style='background: yellow;'>"+search_term+"</span>"); txtMessage.setText(Html.fromHtml(newString ,HtmlCompat.FROM_HTML_MODE_LEGACY)); } else { String newString = text.replaceAll(search_term, "<font color='#f3f402'>"+search_term+"</font>"); txtMessage.setText(Html.fromHtml(newString)); } 
0
source

All Articles