Android strings xml file formatting - android

Hy! I am working on my Android app and I want to format text so that my TextView shows this text on CONTACT US to look EXACTLY like on that website, but I dont know how to format it in strings.xml file. I have tried with basic HTML formatting putting <br> tag for new row and <a> tag for email adresses and web site, but nothing change. How to format it ? Can so large strings be saved into strings.xml ?
http://visitsplit.com/#pages?page=409

You can use \n for new lines.
Further, you can use Html.fromHtml to create a Spanned that would have basic formatting - bold, italic, links, etc.
You would then need to override the movement method to actually make links operational:
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setLinksClickable(true);

You can use HTML tags, like <br> and <a>, by using:
yourTextView.setText(Html.fromHtml(getString(R.string.your_string)));
Make sure you properly escape any special characters in your strings.xml.

Related

How to remove formatting from anchor link in Android strings.xml

I've got a string containing HTML link like this:
<string name="text_with_link"><b>This</b> and <b>this</b> are links</string>
As you noticed, I want the links to be bold. That is the only formatting I want the links to have, though. However, the result looks like this:
How can I remove the underline and color from the link?
I've tried to add style="text-decoration:none;" in following forms
<a href="openDocument://document2.html" style="text-decoration:none;">
<a href="openDocument://document2.html" style="text-decoration:none !important;">
<style>a {text-decoration:none;}</style><b>This</b>
.. but no luck. CSS doesn't seem to work in Android strings.
you will have to use Html.fromHtml for formatting to work like this:
tv = (TextView) findViewById (R.id.textviewid);
tv.setText(Html.fromHtml(getResources.getString(R.string.text_with_link)));
also to change link color you have to use android:textColorLink property in your text view XML to change the link color.
CSS does not work in android strings.

how to superscript in a title in androidplot?

I'm trying to superscript a number in a title in androidplot, like so:
strings file:
<string name="plot_title_3m">stuff per m<sup>3</sup></string>
xml file:
androidPlot.title="#string/plot_title_3m"
but it doesn't superscript at all, the number is normal styling. I've also tried using
<string name="plot_title_3m">stuff per m<sup>3</sup></string>
but no dice, it actually shows the sup tags in the title
also tried this
<string name="plot_title_3m">stuff per m<small><sup>3</sup></small></string>
and actually found out that the 'small' tags don't work either...
You can use HTML tags to achieve this. To be more exact, you need to use Spans on your input text to achieve this look on TextView, but we have a friendly neighborhood helper class that converts some common HTML tags into spans.
E.g.:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

How to make a link INSIDE a string resource (along with other text in it) ? - Android

For example: Take a look at the following string resource:
<string name="b1b">This link will take you to google.com. More text here.</string>
Now I want this string resource to look like this in my app:
This link will take you to Google. More text here.
I can't use three textviews. This was just an example. So I can't make the entire textview a link.
(Why?
What I'm doing in my app is … I have say a dozen buttons, each of them sends a string resource ID as an intent to a "Text Shower Activity" … and in that I simply have a single textview which shows different texts based on which button the user clicked. So, I'm saving on app size.
Plus every such text string resource has different number of links at different places, so it's not feasible to have a single textview just for links and somehow weave it in between.)
So, I need to make a little bit of the string resource into a link. I've tried the <a> thing with no effect.
How to do this?
Example XML resource:
</string name="mlink">
to go to Google<![CDATA[ click here]]>
and <![CDATA[this]]> moves you to yahoo!
</string>
Java:
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
yourTextView.setText(Html.fromHtml(getString(R.string.mlink)));
Note that you need to put your html link inside the CDATA tag, this is the proper way to use links in String resources.
Just to add on #Droidman's answer if anyone wants to use it without CDATA, Below will also work without CDATA but we have to escaped the characters such as "<" , using the < notation.
</string name="mlink">
to go to Google <a href=\"http://google.com\">click here</a>
and <a href=\"http://yahoo.com\">this</a> moves you to yahoo!
</string>
Java:
yourTextView.setMovementMethod(LinkMovementMethod.getInstance());
yourTextView.setText(Html.fromHtml(getString(R.string.mlink)));

HTML formatting for TextView

I am a bit confused about the 'rules' of when a TextView element displays text in formatted form or not.
A string like
"There are <i>different ways</i> of coding.\n";
displays without any formatting (including the HTML codes) when I code
tvMyTextView.setText("There are <i>different ways</i> of coding.\n");
but when I define the same string in strings.xml and then load
tvMyTextView.setText(R.strings.TestString);
it displays emphasized.
Even more confused I feel when trying to embed URLs in TextView's like here:
"Click here to switch on the red light.\n";
Needless to say I already tried the various property options of TextView - but they don't seem to make much of a difference unless I missed something. In some cases the URL is encoded in the text, in blue color and can be clicked, in others I can see the HTML formatting. In others again, it is color-encoded and the URL seems to be encoded in the text somehow - but nothing happens when I click it. Regarding the embedding of URLs, unlike for the other example with 'simple' HTML formatting, I couldn't even find out a rule so far of when it works and when it doesn't. Can anyone help me to untie the knots in my head..
Actually, From the Android Docs..
public final void setText (CharSequence text)
Sets the string value of the TextView. TextView does not accept HTML-like formatting, which you can do with text strings in XML resource files. To style your strings, attach android.text.style.* objects to a SpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.
But,
public final void setText (int resid)
no more specification on it..
But from Android Resource String docs..
You can add styling to your strings with HTML markup. For example:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">Welcome to <b>Android</b>!</string>
</resources>
Supported HTML elements include:
<b> for bold text.
<i> for italic text.
<u> for underline text.
Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String, Object...) method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.
And about your URL string,...
tvMyTextView.setText(Html.fromHtml("Click here to switch on the red light.\n"));
Also look at this SO Question Set TextView text from html-formatted string resource in XML
and
Android String Resource
tvMyTextView.setText(Html.fromHtml("There are <i>different ways</i> of coding.\n"));
also try
Below link For linkify so automatically website link assign.
http://developer.android.com/resources/articles/wikinotes-linkify.html
To add a few notes to my own questions and after having received the answers so far, I can only conclude that there doesn't seem to be a reliable way that works everywhere.
In some cases - according to my experience, if a formatted URL is part of the plain text and not enclosed by tags (like http://www.poon-world.com , even just " Poon-World.com " seems to work in most cases), simply setting the properties of the TextView seems to be enough and the links will be clickable. However, if links are embedded in HTML tags and supposed to be clickable from some link text, there seems to be no other way than to go with Html.fromHtml(..).
But there are also a few special cases I can't explain: in some activities/layouts, I am using "embedded" URLs and have set the Click-properties mentioned before, don't use Html.fromHtml .. and surprise!, a click on the indeed created links in the text indeed opens the browser, but only after having added the following line in the code in the OnCreate-Event:
myTextView.setMovementMethod(LinkMovementMethod.getInstance());
(I found this trick in another thread, thanks to the author) No idea why, it seems to be the way the string resources are parsed and evaluated by Android. I just mentioned that on top of all that's already been said so that everyone else looking for solutions and gets confused doesn't start to think he's starting to lose his mind - no, just test the approaches mentioned here on this page and one should usually work out.

font management

I use Html.fromHtml() in my application to display bold and regular characters in the same TextView. But I have 3 different fonts for italic,bold and regular text, and I don't know how to indicate to my text view to use one or the other.
Please give me any reference or hint.
Thanks in Advance.
if I have not misunderstood you can format a single string with different <font> tag.
For instance :
String toShow = <font: italic ...>italic string</font> <font: bold ...>bold string</font>
Html.fromHtml(toShow)
choose the tag html more appropriate for this purpose. here is a list of supporte html tag.
edit: here an example of the html font tag. As alternative I think you can use SpannableString for the same purpose.

Categories

Resources