How to set part of text to bold when using AlertDialog.setMessage() in Android?

前端 未结 4 1690
别跟我提以往
别跟我提以往 2020-12-29 00:58

How to set part of text to Bold when using AlertDialog\'s setMessage()? Adding and to my Strin

相关标签:
4条回答
  • 2020-12-29 01:16

    In case if anyone wants to add only a single string:

    <string name="abouttxt">&lt;b>Enter license key&lt;/b></string>
    

    Add this line in your Alertdialog code.

    dialog.setTitle(Html.fromHtml(getString(R.string.abouttxt)))
    
    0 讨论(0)
  • 2020-12-29 01:20

    You need to use Html.fromHtml() too. For example:

    AlertDialog.setMessage(Html.fromHtml("Hello "+"<b>"+"World"+"</b>"));
    

    Update:
    Looks like Html.fromHtml(String source) has been deprecated in the Latest Android Nougat version. Although deprecation doesn't mean that you need to change your code now, but it's a good practice to remove deprecated code from your app as soon as possible.
    The replacement is Html.fromHtml(String source, int flags). You just need to add an additional parameter mentioning a flag.

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
       AlertDialog.setMessage(Html.fromHtml("Hello "+"<b>"+"World"+"</b>", Html.FROM_HTML_MODE_LEGACY));
    } else {
       @Suppress("DEPRECATION")
       AlertDialog.setMessage(Html.fromHtml("Hello "+"<b>"+"World"+"</b>"));
    }
    

    For more details have a look at this answer.

    0 讨论(0)
  • 2020-12-29 01:39

    This page describes how to add HTML formatting to resource strings.

    <resources>
        <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.  
        </string>
    </resources>
    

    And do not forget to use: Html.fromHtml

    AlertDialog.setMessage(Html.fromHtml(getString(R.string.welcome_messages)));
    

    This works for me

    0 讨论(0)
  • 2020-12-29 01:43
    <string name="abouttxt">"<b>Info</b>\ntexttxtxtxtxt"</string>
    

    this works for me in xml

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