1

I am trying to put @string/string_b inside html <p></p>. For example,

<string name="string_a">very long text</string>

<string name="string_b">
     <![CDATA[
        <body>
            <p>This is string B</p>
            <p>@string/string_a</p>
            <p>Another string here</p>
        </body>
    ]]>
</string>

This will only shows:
This is string B
@string/string_a
Another string here

But I want to show:
This is string B
very long text.
Another string here

Btw, it will be displayed in HtmlTextView. If can, I want to make it inside xml only. Thank you guys!

3
  • Did you manage to make it work? Commented Jun 9, 2016 at 11:37
  • 1
    Yes but in another way. By extending HtmlTextView. Thanks btw. Commented Jun 9, 2016 at 11:39
  • No problem, post your answer yourself then, so if others have the same issue, they can see the solution. Commented Jun 9, 2016 at 11:40

2 Answers 2

1

Make this change in your strings.xml:

 <string name="string_b">
         <![CDATA[
            <body>
                <p>This is string B</p>
                <p>%1$s</p>
                <p>Another string here</p>
            </body>
        ]]>
    </string>

Now in you Java code you can do this:

Resources res = getResources();
String string_a = res.getString(R.string.string_a);
String string_b = String.format(res.getString(R.string.string_b), string_a);

Upon your request to do it solely in the xml, I've found this question that refers to the same thing. So you can take a look at the answer given there and see if it works for you.

Sign up to request clarification or add additional context in comments.

2 Comments

is it possible to make it only in xml?
Take a look at the question and the answer to it, the one I linked in the edit.
0

This is my answer. Hope it help others. Extending HtmlTextView gives us opportunity to add new attribute with string format. Then, android:text and custom:text2 can be concatenated programatically inside custom HtmlTextView.

1. Extend HtmlTextView or TextView if you are using it.

public class HtmlTextview extends HtmlTextView {
    public HtmlTextview(Context context) {
        super(context);
    }

    public HtmlTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupText(attrs);
    }

    public HtmlTextview(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setupText(attrs);
    }

    private void setupText(AttributeSet attrs) {

        String htmlString = (String) this.getText();
        String text2;

        TypedArray attributeValuesArray = getContext().obtainStyledAttributes(attrs, R.styleable.concatenate, 0, 0);
        try{
            text2 = attributeValuesArray.getString(R.styleable.concatenate_text2);
        } finally {
            attributeValuesArray.recycle();
        }

        if(text2 != null && text2.length() > 0){
            htmlString = htmlString + text2;
            this.setHtmlFromString(htmlString, new HtmlTextView.RemoteImageGetter());
        }
    }
}

2. HtmlTextview attributes

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="concatenate">
        <attr name="text2" format="string"/>
    </declare-styleable>

</resources>

3. In string.xml resources

<resources>
    <string name="app_name">HtmlTry</string>

    <string name="string_a">
        <![CDATA[
        <body>
            <p>very long text. Can also formatted in <font color="#f44336">Html.</font></p>
        </body>
        ]]>
    </string>

    <string name="string_b">
        <![CDATA[
        <body>
            <p>This is string B</p>
        </body>
        ]]>
    </string>

</resources>

4. Lastly apply it inside xml layout.

<com.htmltry.HtmlTextview
   android:id="@+id/tv"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="@string/string_b"
   custom:text2="@string/string_a"
/>

and the final result is: enter image description here

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.