Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change text Background color of Specific word in a TextView

Tags:

android

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

Like image below

enter image description here

like image 246
M.Muzammil Avatar asked Dec 04 '15 07:12

M.Muzammil


People also ask

How do I change the background color of a string?

Are you talking about string indicator and string control? If so, right-click on a string control/indicator and select Create -> Property Node -> Text -> Text Colors -> BG Color. Change it to write and wire a number of the color you want to use.

How can I change the color of a part of a TextView in Android?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

How do we change a color of a text in Android Studio?

There are two methods of changing the color of a TextView and the code for that has been given in both Java and Kotlin Programming Language for Android, so it can be done by directly adding a color attribute in the XML code or we can change it through the MainActivity File.

What is Spannable string in Android?

Text styling is one of the important aspects when it comes to enhancing the UI of an Android application. In Android, we can change the size, color, weight, style, etc of a text and make the text more attractive and appealing.


2 Answers

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

     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 like the following:

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

enter image description here

like image 179
BNK Avatar answered Nov 10 '22 05:11

BNK


    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 & How it works ? Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere. For more details please you can check setSpan of Spannable & also know how that start and end number is required (like in anser 3 & 7 in BackgroundColorSpan

UPDATE

Please 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);
}
like image 37
Nitin Mesta Avatar answered Nov 10 '22 05:11

Nitin Mesta