0

I am developing an android app and i was wondering how i can change the color of a particular link inside a XML string.

This in strings_example.xml

<string name="hello_there">Hello There!<a href="tel:XXXXXXXXX" color="#8ABD37>More info!</a></string>

This is my activity layout

<TextView
    somecode
    android:text="@strings/hello_there"
    style="@styles/whatever"/>

So, how can i apply "Whatever style" without overriding the color inside the xml string?

Also: I cannot place the style inside the xml string, i have to style it from the TextView

4 Answers 4

1

You can use Html styling property like below

use <![CDATA[ ...raw html... ]]> inside your string.xml

<string name="formated_string"><![CDATA[ <p Hello there <b>More info</b> and <i>Here</i </p>]]> </string>

Inside your java class

TextView myTv = (TextView)findViewById(R.id.foo);
myTv.setText(Html.fromHtml(getString(R.string.formated_string)));

You can add custom html style acording your needs

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

2 Comments

Thanks for the comment! But i have serious doubts about it, @vipul-prajapati If you do: myTv.setTextAppareance(...) I am sure you are going to override your Html string. Is it?
@DavidFernándezEsteban i didn't use setTextAppareance(...) in my solution, also this method depricated from API 23.
0

There is two way for styling text in android:

1- You can use SpannableString and split your text into multiple parts like this:

val spannable = SpannableString("Sample Text.......")
spannable.setSpan(ForegroundColorSpan(Color.RED), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
myTextView.text = spannable

2- or you can render html in your textview in this way:

textView.setText(Html.fromHtml(
    "Hello There!<a href=\"tel:XXXXXXXXX\" color=\"#8ABD37\">More info!</a>"));

//or

textView.setText(Html.fromHtml(getResources().getString(R.string.hello_there)));

2 Comments

Thanks for the response! But, i was searching a way of combining style of Texview with the Html color of the string xml
second approach is what you want. you can put html code directly in your textview and it will be rendered in the correct way.
0

I've mad a bit complex solution. I am replacing current URLSpan with custom one.

TextView textView = findViewById(R.id.textView);

// Make the link to be clickable
textView.setMovementMethod(LinkMovementMethod.getInstance());

// Url link
Spanned text = Html.fromHtml(getString(R.string.hello_there));

// We search for the first occurrence of URLSpanand we replace it with custom one
SpannableStringBuilder spannable = new SpannableStringBuilder(text);
Object[] spans = spannable.getSpans(0, spannable.length(), Object.class);
for (Object span : spans) {
    if (span instanceof URLSpan) {
        int spanStart = spannable.getSpanStart(span);
        int spanEnd = spannable.getSpanEnd(span);
        int spanFlags = spannable.getSpanFlags(span);

        URLSpan spanUrlBase = (URLSpan) span;
        MyUrlSpan myUrlSpan = new MyUrlSpan(spanUrlBase.getURL());

        spannable.setSpan(myUrlSpan, spanStart, spanEnd, spanFlags);
        break;
    }
}

textView.setText(spannable);

--

private class MyUrlSpan extends URLSpan {

    public MyUrlSpan(String url) {
        super(url);
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setColor(Color.GREEN);
    }
}

--

Xml doesn't recognize all special chars. I had to decode < to proper declaration.

<string name="hello_there">&lt;a color="#8ABD37" href="tel:XXXXXXXXX">More info!&lt;/a></string>

More info here.

Comments

0

I hate to answer myself. This is the best way:

<style name="generic_body_style_with_links" parent="generic_body_style">
        <item name="android:textColorLink">@color/green_400</item>
</style>

In this way, you can set the color of regular text to black in your generic_body_style, for example, and you can set the color of <a href =""..../> to green. All inside the same TextView without splitting it.

The other things like are useful but you cannot apply it in this case because you are overriding this color with the default color of your TextView´s style.

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.