SoFunction
Updated on 2025-04-06

How to use TextView in Android UI

How to use TextView in Android UI

1. Set colors, sizes, and click events in different regions of TextView

String msg = getResources().getString(.school_roll_auth); 
    SpannableStringBuilder style = new SpannableStringBuilder(msg); 
    // Set font color    ( 
        new ForegroundColorSpan(getResources().getColor( 
            .tv_color_99)), 0, () - 5, 
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
    ( 
        new ForegroundColorSpan(getResources().getColor( 
            .text_blue)), () - 5, (), 
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
    // Set the font background color    ( 
        new BackgroundColorSpan(getResources().getColor( 
            .bg_common)), () - 5, (), 
        Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
    // Set the clickable part    (new ClickableSpan() { 
 
      @Override 
      public void onClick(View widget) { 
        //do Click .... 
      } 
 
      @Override 
      public void updateDrawState(TextPaint ds) { 
        (ds); 
        (false); 
        (); 
      } 
 
    }, () - 5, (), Spannable.SPAN_EXCLUSIVE_INCLUSIVE); 
    tv_school_roll_msg.setText(style); 
    // This method is used when you need to respond to user events, such as clicking on a phone number and jumping to the dialing page.  If this method is not executed, the event will not be responded to, even if the text looks like it is underlined blue.    tv_school_roll_msg.setMovementMethod(()); 
//Set different font sizesSpannableStringBuilder sb = new SpannableStringBuilder(numText); 
 
      (new AbsoluteSizeSpan(().getResources() 
          .getDimensionPixelSize(.text_size_24)), 0, 
          (".") + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      (new AbsoluteSizeSpan(().getResources() 
          .getDimensionPixelSize(.text_size_16)), 
          (".") + 1, (), 
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      (sb); 

Note:The Spannable.SPAN_EXCLUSIVE_EXCLUSIVE parameter is an explanation of the region, including the left and the right, not the append method of SpannableStringBuilder

SpannableStringBuilder sb = new SpannableStringBuilder(num); 
SpannableStringBuilder sbUnitName = ("Yuan"); 
(new AbsoluteSizeSpan(getResources().getDimensionPixelSize(floatSizeResourcesId)), () - 1, () + (),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

After append, the index of the number character does not start from 0, but from the original character.

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!