PeakGen
PeakGen

Reputation: 23025

Alert Dialog is not displaying HTML defined in strings.xml

Please have a look at the following code

strings.xml

<string name="measurements_description"><html><font color="#FFFAF0">I want to have white color for the whole text. 
    <br/>This text comes in a new line<font color="red">This text is red </font>
    <br/>This is another line. Again, color is white </font></html></string>

male_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <RelativeLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
        <TextView 
            android:id="@+id/measurementsDescription"
            android:text="@string/measurements_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
    </RelativeLayout>
</ScrollView>

Form.java (This is belongs to the main layout activity_form)

public boolean onMenuItemSelected(int id, MenuItem item) {
    //Toast.makeText(this, item.getTitle(), Toast.LENGTH_LONG).show();
    
    TextView msg = new TextView(this);
    msg.setText("<html><u>Message</u></html>");
        
    AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
    //alertBuilder.setMessage("<html><u>Message</u></html>");
    alertBuilder.setTitle("Male Measurement Instructions");
    LayoutInflater li = getLayoutInflater();
    View v = li.inflate(R.layout.male_layout, null);
        
    alertBuilder.setView(v);
    alertBuilder.setCancelable(false);
    alertBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface arg0, int arg1) {
             //alertBuilder.finish();
        }
    });
        
    AlertDialog alertDialog = alertBuilder.create();
    alertDialog.show();
        
    return true;
}

This is how it gets displayed, without responding to any of the HTML in the string.xml file. Why this is not changing it's colors as coded in HTML? Why it is not keeping line breaks as coded in HTML?

enter image description here

How to solve this issue? Please help!

Upvotes: 0

Views: 1671

Answers (3)

Laurent
Laurent

Reputation: 456

You can use HTML formatting + format string, by declaring a string in strings.xml:

<string name="key">
    <![CDATA[
    My text and tags <b>with or without</b> variables<br/><br/>
    Answer is <u>%1$s</u>
    ]]>
</string>

And create your dialog:

String msg = String.format(getResources().getString(R.string.key), "42");
Spanned htmlMsg = Html.fromHtml(msg);
builder.setMessage(htmlMsg).show();

Upvotes: 1

sabadow
sabadow

Reputation: 5151

The Android Documentation says that only a few HTML tags (including <b>, <i>, <u> ) are supported by the TextView.

If you want to show words of different colors you should use SpannableString

A complete example here.

Hope it helps.

Edit:

Other way to do this:

String text = "<font color=#cc0029>Erste Farbe</font> <font color=#ffcc00>zweite Farbe</font>";
yourtextview.setText(Html.fromHtml(text));

Upvotes: 1

Panos Bariamis
Panos Bariamis

Reputation: 4653

First problem is the nested <font>. It won't work if you have <font> inside an other <font>. You can set the android:textColor attribute to #FFFFFF and then with the <font> you can change the color of some text. Second you have to use CDATA in your string and third you have to set the text of the TextView from code so you can format the string with html. So you must have

male_layout.xml

...
<TextView 
    android:id="@+id/measurementsDescription"

    android:textColor="#FFFFFF"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
...

strings.xml

<string name="measurements_description">
<![CDATA[
    I want to have white color for the whole text. 
    <br/>This text comes in a new line <font color="red">This text is red </font>
    <br/>This is another line. Again, color is white
]]>    
</string>

Form.java

...
String html = getString(R.string.measurements_description);
TextView tv = (TextView) findViewById(R.id.measurementsDescription);
tv.setText(Html.fromHtml(html));
...

Upvotes: 3

Related Questions