Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text in Bold in AlertDialog

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();


    }
like image 294
Anchit Mittal Avatar asked Nov 30 '13 07:11

Anchit Mittal


2 Answers

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.

like image 169
robnick Avatar answered Sep 20 '22 11:09

robnick


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

like image 29
Arun Kumar Avatar answered Sep 22 '22 11:09

Arun Kumar