Im trying to format items(Bold, new line, italics, etc...) In a string array in an xml file Ive created for the arrays(items.xml). Each Item is going to have a different format and Im trying to figure out a way to format each item. using \n I can get new lines, but can get bolding of text or anything else. How can this be done?
My code to display the items in the array:
Intent launchingIntent = getIntent();
String content = launchingIntent.getData().toString();
TextView viewer = (TextView) findViewById(R.id.tutView);
viewer.setText(content);
setContentView(viewer);
It grabs the item number from a previous activity to display the text in the item in the array.
EDIT: Found the answer. Cant answer it for another 8 hours, so here it is:
I figured out, by searching the Android dev website, which I should have done first, is to use "<" before the bold tags, and to use Html.fromHtml(content) and it seems to do exactly what I needed.
Styling with HTML markup
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>
Similar thing can be applied with string array in strings.xml
Supported HTML elements include:
<b> for bold text.
<i> for italic text.
<u> for underline text.
Related
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>"));
I want to display help dialog box in which it has one textview and it loads the content from the String.xml file. Instead of making it one boring paragraphs, I would like to add some formatting to that String.xml For example coloring some sentences, bold..etc. Is there a way I can do that in the xml file within the string?
My xml looks like that
<string name="help_summary">Clicking on button (Summary) will result in ((report))</string>
So I want (Summary) to be red color and ((report)) to be bold.
How can I achieve that?
You can use Html. When you load the string in the textview use:
yourTextView.setText(Html.fromHtml(context.getResources().getString(R.string.help_summary)));
And use html in the string, for example:
<string name="help_summary"><![CDATA[Clicking on button <span style="color:red">Summary</span> will result in <b>report</b>]]></string>
There are some attributes that can be used in string resources without implementing HTML into and or altering your java code. None needed, you CAN do most of what you seem to need in strings.xml
Example:
<string name ="my_string"><i>italics</i><b>bold</b><u>underline</u><font fgcolor="#FFFFFFFF">color</font><small>small text</small></string>
I believe there is a <large/> or <big/> tag as well, amongst a few more.
Edit also, there is \n for a new line. Just include that in your string like so:
<string name="paragraph">Text is one first line \n now text is on next line. \n\n this will appear as an indented paragraph now.</string>
Hope this helps, happy coding!
<string name="some_text">this is <font fgcolor="#ffff0000">red</font></string>
or
<string name="some_text">this is <font color="#ffff0000">red</font></string>
is not doing it.
I need to call the string in an xml TextView. How do I get it to work? No I don't want to use a horizontal LinearLayout with a bunch of TextViews.
You can't do this. instead of you can set TextView text as HTML like
textview.setText(Html.fromHtml("this is<font color=\"##ffff0000\">red</font>"));
Go to this for more HTML tag support information :html-tags-supported-by-textview
TextView doesn't support entering HTML text directly, though some of the other answers seem to show you how to get a Html object from the String. If you want to format the text within a TextView you need to use Spannable instances to format the sections of text to have different colours, fonts etc. This is only good if you don't already have your text in HTML though, of course.
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.
I'm trying to make an app with localisation built in, but I want a way that I can create a web link within the text, the URL being defined elsewhere (for ease of maintenance).
So, I have my links in res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="link1">http://some.link.com</string>
<string name="link2">http://some.link2.com</string>
</resources>
and my localised text in res/values-en-rGB/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
...
<string name="sampleText">Sample text\nMore text and link1\nMore text and link2.</string>
</resources>
I've not tested this bit, but from the localization section of developer.android.com it says that this approach to reducing content duplication should work, although I'm not sure what folder I should put Italian, for example. Would it be in 'res/values-it-rIT/strings.xml'? Lets assume that I have various other languages too.
I'm looking for a way of taking the base localised 'sampleText' and inserting my html links in, and getting them to work when clicked on. I've tried two approaches so far:
1,
Putting some formatting in the 'sampleText' (%s):
<string name="sampleText">Sample text\nMore text and link1\nMore text and link2.</string>
and then processing the text like this:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(getResources().getString(R.string.sampleText, getResources().getString(R.string.link1), getResources().getString(R.string.link2)));
But this didn't work when I click on the link, even though the link text is being put in to the correct places.
2, I tried to use Linkify but the regular expression route may be difficult as I'm looking at supporting non-Latin based languages. I tried to put a custom xml tag around the link text and then do something like this:
Pattern wordMatcher = Pattern.compile("<span1>.*</span1>");
String viewURL = "content://" + getResources().getString(R.string.someLink);
Linkify.addLinks(tv, wordMatcher , viewURL );
But this didn't work either.
So, I'd like to know if there's a way of dynamically adding multiple URLs to different sections of the same text which will link to web content?
The problem is your "a href" link tags are within strings.xml and being parsed as tags when strings.xml is parsed, which you don't want. Meaning you need to have it ignore the tags using XML's CDATA:
<string name="sampleText">Sample text <![CDATA[link1]]></string>
And then you can continue with Html.fromHtml() and make it clickable with LinkMovementMethod:
TextView tv = (TextView) findViewById(R.id.textHolder);
tv.setText(Html.fromHtml(getString(R.string.sampleText)));
tv.setMovementMethod(LinkMovementMethod.getInstance());
In your layout set android:autoLink to web
<TextView android:text="#string/text_with_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="web" />
And in strings.xml just add the URL(s).
<string name="text_with_url">http://stackoverflow.com/ FTW!</string>
Try using Html.fromHtml() to convert the HTML into a Spannable that you put into the TextView. With what you have in #1, I would expect the TextView to show the HTML source, not rendered HTML.
You have to implement
setMovementMethod(LinkMovementMethod.getInstance());
on your Textview
Here is a better example:
clickable-urls-in-android-textviews