0

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!

6
  • You mean display <html> and other tags directly in dialog? Commented Feb 4, 2013 at 7:30
  • @Glenn: I mean, they are not doing what they should do. Like converting color, adding line breaks, etc. Commented Feb 4, 2013 at 7:32
  • HTML Tag <br /> is not supported but you can use \n. For coloring, you can set the textview's font color like Color.BLACK or parse a color like Color.parseColor("#FFFAF0"). Commented Feb 4, 2013 at 7:37
  • You can also try WebView Commented Feb 4, 2013 at 7:39
  • OK, so in that case I have to edit it, inside te layout.xml file. But, there is a place in the text where the colour is getting changed to "red" only for few words. Please help Commented Feb 4, 2013 at 7:58

3 Answers 3

3

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));
...
Sign up to request clarification or add additional context in comments.

2 Comments

I tried it. Setting it inside Form.java is not working. An error is getting generated
I implemented it before i answer, so i know that it works. Post the error so i can help you. And some code of course. Make an edit in your question
1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.