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

android, android-alertdialog, java

Solution

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.

Problem

How to set part of text to Bold when using `AlertDialog`'s `setMessage()`? Adding `<b>` and `</b>` to my `String` doesn't work.

Original source

Related problems