John J Smith
John J Smith

Reputation: 11931

Html format tags ignored in Dialog

A few Html tags in the strings.xml file are rendered properly when used in a TextView, so for example, the following text resource would appear bold:

<string name="example_text"><b>This text is bold</b></string>

However, if the same text is used in a custom Dialog, the formatting is ignored.

Does anyone know how to format part of the text in a scrollview within a dialog box?

Upvotes: 8

Views: 1896

Answers (1)

Neil Hoff
Neil Hoff

Reputation: 2085

You could format with HTML by using a WebView in the dialog:

strings.xml

<string name="example_text" formatted ="false"><![CDATA[ <strong> Example Text </strong> ]]></string>

java

String string = getString(R.string.example_text);
WebView wv = new WebView (getBaseContext());
wv.loadData(string, "text/html", "utf-8");
wv.setBackgroundColor(Color.WHITE);
wv.getSettings().setDefaultTextEncodingName("utf-8");
new AlertDialog.Builder(this)
 .setCancelable(false)
 .setView(wv)
 .setNeutralButton("OK", new DialogInterface.OnClickListener(){
    @Override
    public void onClick(DialogInterface dialog, int which) {
        dialog.cancel();

    }

  }) 
 .show(); 

Upvotes: 8

Related Questions