Bold words in a string of strings.xml in Android

Bold words in a string of strings.xml in Android

Asked on January 8, 2019 in XML.
Add Comment


  • 6 Answer(s)

         Normally use html tags in the string resource like below,

    <resource>
       <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
    </resources>
    

         Also use Html.fromHtml else use spannable.

    Answered on January 8, 2019.
    Add Comment

         Try the given codes that uses html tag inside string resources,

    <resources>
    <string name="string_resource_name"><![CDATA[<b> Your text </b>]]> </string>
    </resources>
    

         Get bold text from string resources like,

    private Spanned getSpannedText(String text) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT);
         } else {
            return Html.fromHtml(text);
         }
      }
    String s = format(context.getResources().getString(R.string.string_resource_name));
    textView.setText(getSpannedText(s));
    
    Answered on January 8, 2019.
    Add Comment

    Use the HTML in string resources:

    <resource>
       <string name="my_string">A string with <i>actual</i> <b>formatting</b>!</string>
    </resources>
    

         Later use the getText(R.string.my_string) method alternative to getString(R.string.my_string), then user may get back a CharSequence alternative than a string which consists the formatting embedded.

    Answered on January 8, 2019.
    Add Comment
    <resource>
        <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
    </resources>
    Answered on February 25, 2019.
    Add Comment
    private Spanned getSpannedText(String text) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                return Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT);
            } else {
                return Html.fromHtml(text);
            }
        }
    
    
     String s = format(context.getResources().getString(R.string.string_resource_name));
     textView.setText(getSpannedText(s));
    Answered on February 25, 2019.
    Add Comment
    <resources xmlns:tools="http://schemas.android.com/tools">
    
     <string name="total_review"><b>Total Review: </b> </string>
    
     </resources>
    Answered on February 25, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.