Reputation: 23
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
Upvotes: 0
Views: 192
Reputation: 23
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.
Upvotes: 0
Reputation: 12304
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"><a color="#8ABD37" href="tel:XXXXXXXXX">More info!</a></string>
More info here.
Upvotes: 0
Reputation: 562
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)));
Upvotes: 0
Reputation: 1183
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
Upvotes: 1