Highlighting Text Color using Html.fromHtml() in Android?

后端 未结 9 1295
忘了有多久
忘了有多久 2020-11-28 19:26

I am developing an application in which there will be a search screen where user can search for specific keywords and that keyword should be highlighted. I have found Html.f

相关标签:
9条回答
  • 2020-11-28 20:02

    font is deprecated use span instead Html.fromHtml("<span style=color:red>"+content+"</span>")

    0 讨论(0)
  • 2020-11-28 20:03

    Using color value from xml resource:

    int labelColor = getResources().getColor(R.color.label_color);
    String сolorString = String.format("%X", labelColor).substring(2); // !!strip alpha value!!
    
    Html.fromHtml(String.format("<font color=\"#%s\">text</font>", сolorString), TextView.BufferType.SPANNABLE); 
    
    0 讨论(0)
  • 2020-11-28 20:06

    Or far simpler than dealing with Spannables manually, since you didn't say that you want the background highlighted, just the text:

    String styledText = "This is <font color='red'>simple</font>.";
    textView.setText(Html.fromHtml(styledText), TextView.BufferType.SPANNABLE);
    
    0 讨论(0)
  • 2020-11-28 20:07

    First Convert your string into HTML then convert it into spannable. do as suggest the following codes.

     Spannable spannable = new SpannableString(Html.fromHtml(labelText));
                        
    spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color)), spannable.toString().indexOf("•"), spannable.toString().lastIndexOf("•") + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                
    
    0 讨论(0)
  • 2020-11-28 20:09

    To make part of your text underlined and colored

    in your strings.xml

    <string name="text_with_colored_underline">put the text here and &lt;u>&lt;font color="#your_hexa_color">the underlined colored part here&lt;font>&lt;u></string>
    

    then in the activity

    yourTextView.setText(Html.fromHtml(getString(R.string.text_with_colored_underline)));
    

    and for clickable links:

    <string name="text_with_link"><![CDATA[<p>text before link<a href=\"http://www.google.com\">title of link</a>.<p>]]></string>
    

    and in your activity:

    yourTextView.setText(Html.fromHtml(getString(R.string.text_with_link)));
    yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
    
    0 讨论(0)
  • 2020-11-28 20:10

    This can be achieved using a Spannable String. You will need to import the following

    import android.text.SpannableString; 
    import android.text.style.BackgroundColorSpan; 
    import android.text.style.StyleSpan;
    

    And then you can change the background of the text using something like the following:

    TextView text = (TextView) findViewById(R.id.text_login);
    text.setText("");
    text.append("Add all your funky text in here");
    Spannable sText = (Spannable) text.getText();
    sText.setSpan(new BackgroundColorSpan(Color.RED), 1, 4, 0);
    

    Where this will highlight the charecters at pos 1 - 4 with a red color. Hope this helps!

    0 讨论(0)
提交回复
热议问题