Formatting a Substring in TextView

Nathan Sokalski 4,111 Reputation points
2021-10-24T01:14:13.907+00:00

I have a TextView for which I want a single character to be a different color. The entire string is static, it will not change. I have found several things about different types of spans and using html, but I want to (if possible) keep everything in the layout file (or strings.xml). Is there any way to change the color of only a substring of my TextView using only xml? Thanks.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Answer accepted by question author
  1. JarvanZhang 23,971 Reputation points
    2021-10-25T05:41:44.453+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You could add the text content to strings.xml and set the textColor for the different text separately.

    Here is the sample code, you could refer to it.

       <TextView  
           android:id="@+id/textView1"  
           android:text="@string/test_content"  
           android:layout_width="match_parent"  
           android:layout_height="wrap_content"/>  
    

    Resource\values\strings.xml

       <resources>  
         <string name="test_content">  
           normal text content  
           <font fgcolor="#ffff0000">Hello world!</font>  
         </string>  
       </resources>  
    

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.