Set color of TextView span in Android

前端 未结 15 1492
眼角桃花
眼角桃花 2020-11-22 05:54

Is it possible to set the color of just span of text in a TextView?

I would like to do something similar to the Twitter app, in which a part of the text is blue. See

相关标签:
15条回答
  • 2020-11-22 06:15

    Below works perfectly for me

        tvPrivacyPolicy = (TextView) findViewById(R.id.tvPrivacyPolicy);
        String originalText = (String)tvPrivacyPolicy.getText();
        int startPosition = 15;
        int endPosition = 31;
    
        SpannableString spannableStr = new SpannableString(originalText);
        UnderlineSpan underlineSpan = new UnderlineSpan();
        spannableStr.setSpan(underlineSpan, startPosition, endPosition, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    
        ForegroundColorSpan backgroundColorSpan = new ForegroundColorSpan(Color.BLUE);
        spannableStr.setSpan(backgroundColorSpan, startPosition, endPosition, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    
        StyleSpan styleSpanItalic  = new StyleSpan(Typeface.BOLD);
    
        spannableStr.setSpan(styleSpanItalic, startPosition, endPosition, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    
        tvPrivacyPolicy.setText(spannableStr);
    

    Output for above code

    0 讨论(0)
  • 2020-11-22 06:19

    Another answer would be very similar, but wouldn't need to set the text of the TextView twice

    TextView TV = (TextView)findViewById(R.id.mytextview01);
    
    Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");        
    
    wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    TV.setText(wordtoSpan);
    
    0 讨论(0)
  • 2020-11-22 06:22

    I always find visual examples helpful when trying to understand a new concept.

    Background Color

    SpannableString spannableString = new SpannableString("Hello World!");
    BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
    spannableString.setSpan(backgroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString);
    

    Foreground Color

    SpannableString spannableString = new SpannableString("Hello World!");
    ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
    spannableString.setSpan(foregroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString);
    

    Combination

    SpannableString spannableString = new SpannableString("Hello World!");
    ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
    BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
    spannableString.setSpan(foregroundSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    spannableString.setSpan(backgroundSpan, 3, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(spannableString);
    

    Further Study

    • Explain the meaning of Span flags like SPAN_EXCLUSIVE_EXCLUSIVE
    • Android Spanned, SpannedString, Spannable, SpannableString and CharSequence
    0 讨论(0)
  • 2020-11-22 06:22

    Here's a Kotlin Extension Function I have for this

        fun TextView.setColouredSpan(word: String, color: Int) {
            val spannableString = SpannableString(text)
            val start = text.indexOf(word)
            val end = text.indexOf(word) + word.length
            try {
                spannableString.setSpan(ForegroundColorSpan(color), start, end,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
                text = spannableString
            } catch (e: IndexOutOfBoundsException) {
             println("'$word' was not not found in TextView text")
        }
    }
    

    Use it after you have set your text to the TextView like so

    private val blueberry by lazy { getColor(R.color.blueberry) }
    
    textViewTip.setColouredSpan("Warning", blueberry)
    
    0 讨论(0)
  • 2020-11-22 06:25

    Set your TextView´s text spannable and define a ForegroundColorSpan for your text.

    TextView textView = (TextView)findViewById(R.id.mytextview01);    
    Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");          
    wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
    textView.setText(wordtoSpan);
    
    0 讨论(0)
  • 2020-11-22 06:26

    Set Color on Text by passing String and color:

    private String getColoredSpanned(String text, String color) {
      String input = "<font color=" + color + ">" + text + "</font>";
      return input;
    }
    

    Set text on TextView / Button / EditText etc by calling below code:

    TextView:

    TextView txtView = (TextView)findViewById(R.id.txtView);
    

    Get Colored String:

    String name = getColoredSpanned("Hiren", "#800000");
    

    Set Text on TextView:

    txtView.setText(Html.fromHtml(name));
    

    Done

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