7

I want to make a part of message text in Bold in AlertDialog.

I tried:

adding <b> </b> tag in strings.xml but nothing positive.

i have also used Html.fromHtml("<b>"+getString(R.string.ittformulanote)+"</b>")

i have also been to stackoverflow.com but no positive result.

Below my Code:

 showDialog(getActivity(),"Sample",Html.fromHtml("<b>"+getString(R.string.ittformulanote)+"</b>")+"\n\n"+));



public static void showDialog(Context mContext, String Title,
            String Description) {

        final AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

        dialog.setTitle(Title);
//      dialog.setMessage((Html.fromHtml("<b>"+Description+"</b>")));
        dialog.setMessage(Description);
        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                // TODO Auto-generated method stub

            }
        });

        //

        AlertDialog alert=dialog.create();
//      dialog.show();
        alert.show();


    }
Community
  • 1
  • 1
Anchit Mittal
  • 3,412
  • 4
  • 28
  • 48

7 Answers7

8

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

Their example seems to help if you are having trouble with formatting strings:

Store your styled text resource as an HTML-escaped string:

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

In this formatted string, a element is added. Notice that the opening bracket is HTML-escaped, using the < notation.

robnick
  • 1,720
  • 17
  • 27
  • 2
    Just to be clear, you also need Html.fromHtml() in your Java code, in addition to escaping the < as described here. Thanks for the solution! – Sterling Jun 08 '14 at 17:00
  • i checked lots of solutions about this but this is the excatly working solution. – savepopulation May 10 '16 at 07:26
  • This works for AlertDialog when used with String content = getString(R.string.welcome_messages); if(android.os.Build.VERSION.SDK_INT >=android.os.Build.VERSION_CODES.N) { text = Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY ); }else { text = Html.fromHtml(content); } – Vie Jul 03 '19 at 06:09
4
AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(Html.fromHtml("<b>"+getString(R.string.ittformulanote)+"</b>"));
        builder.setNeutralButton("OK", new OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub

            }
        });
        AlertDialog alert = builder.create();
        alert.show();

Please Try this code..Its working fine

Arun Kumar
  • 100
  • 1
  • 2
  • 12
1

You need to set bold text in xml like:

<resource>
<string id="@+id/your_message">We are <b><i>so</i></b> glad to see you.</string>
</resources>

And call it at particular place where you want text in bold like:

Html.fromHtml("<b>"+getString(R.string.your_message)+"</b>")
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0
<string name="demoStr"><Data><![CDATA[ <b>ABC</b> ]]> </Data></string>
WIllJBD
  • 6,144
  • 3
  • 34
  • 44
0
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="welcome">Welcome to <b>Android</b>!</string>
</resources>
vinay Maneti
  • 1,447
  • 1
  • 23
  • 31
0

I solved it by creating my own textView in AlertDialog.Builder.

LayoutInflater inflater = LayoutInflater.from(getContext());
View view = inflater.inflate(R.layout.dialog_custom_text_view, null);
TextView textView = view.findViewById(R.id.dialog_custom_text_view);
textView.setText(message);
builder.setView(view);
Vít Kapitola
  • 499
  • 1
  • 5
  • 9
0

To make the title and message bold, you can do this.

showDialog(getActivity(),"Sample",Html.fromHtml("<b>"+getString(R.string.ittformulanote)+"</b>")+"\n\n"+));


public static void showDialog(Context mContext, String Title, String Description) {

    final AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
    dialog.setTitle(Title);

    // Pass description to placeholder string
    dialog.setMessage(
        (Html.fromHtml("<b>" + getString(R.string.message_placeholder, Description) + "</b>"))
    );

    dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            // TODO Auto-generated method stub
        }
    });


    AlertDialog alert=dialog.create();
    alert.show();
}



<resources>
    <string name="ittformulanote">&lt;b>Bold text note&lt;/b></string>
    <string name="message_placeholder">&lt;b>%1$s&lt;/b></string>
</resources>
David Kariuki
  • 1,522
  • 1
  • 15
  • 30