http://docs.fusioncharts.com/charts/contents/Styles/Font.html
I tried this, along with a lot of things but failed to do so.
Here's what I want.
<string name="ss">Bold. Underlined. Italic. Big. Small</string>
I want to format a little bit of the string.
Where it's written bold, I want it to be bold...and same for others.
I tried a lot of tags ...but well nothing worked, and I couldn't find anything on Google or SO.
I know how to do it in a textview, but that's not what I want...
I'm sending some text resource to an activity that shows it...
If I did it with different text views, I'd have to create several of them, a new one for whenever I want bold text, and that's not very elegant.
Is there a way to simple do this in the XML file ? or some other way ?
Try wrapping your marked up text in CDATA tags. For example:
<string name="ss"><![CDATA[<b>Bold.</b> <u>Underlined.</u> <i>Italic.</i> <big>Big.</big> <small>Small</small>]]></string>
And then use Html.fromHtml wherever you're wanting to display it:
Html.fromHtml(getString(R.string.ss))
This problem has been driving me crazy for ages. It's something sooo simple that you just want it to work!!!
Anyway I've found an answer here at http://www.coderzheaven.com/2011/06/19/styling-text-in-android-through-xml/
The key is to load the resource as a CharSequence using getResources().getText(R.string.xxxx) this will retain all the style information and allow you to use inline styling tags.
My mistake was using getString() because when loading your resource getString() will cause the string to lose all its style information.
exemple:
<string name="ss"><font size="15"><b>Parrainage</b></font><u>subscribe</u></string>
b = bold et u = underline .....etc
This is working for me.
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
txt.setText(Html.fromHtml(getString(R.string.welcome_messages)));
more details check Official site:
https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithSpannables
in dimens file write:
<dimen name="size_edittext">180dp</dimen>
and in your xml layout or activity call it:
android:#dimen/ size_edittext
Related
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!
I want to build a help page for my app so obviously it is gona be texty. Other than putting those texts inside my string.xml or in my layout xml, is there any other ways for me to do it? how do I include paragraphs in them? any help would be very much appreciated!
You should put your help text into strings.xml file and use \n\n for line break as #alex suggested because in future if you want help text in different language then you can easily do this by adding different strings.xml file for different localization
But if you still do not want to put help text into strings.xml then you can use
static final String helpText = "help text";
and set help text into textView.
Put your text strings in strings.xml.
Reference them in your layout's XML.
Use \n\n to mark the beginning of a new paragraph.
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 am trying to add lines with different colors to my TextView using html tags.
For whatever reason,
Html.fromHtml("<font color='#145A14'>text</font>");
won't show up colored in the TextView.
Html.fromHtml("<font color='#145A14'>text</font>");
Instead of above please use following
Html.fromHtml("<![CDATA[<font color='#145A14'>text</font>]]>");
This worked for me and I am sure it will also work for you.
Let me know in case of any issue.
My answer involves guesswork about your code, but here goes:
When you use the font tag: DO NOT include an alpha channel so that your hex string looks like "#ff123456". If you use Integer.toHexString(), you will have an alpha channel in that result.
It worked when i used substring(2) on my hex string from rescource.
To sum up:
text.setText(Html.fromHtml("<font color='#123456'>text</font>"));
will work, but:
text.setText(Html.fromHtml("<font color='#ff123456'>text</font>"));
won't!
Make sure to turn off any modifiers like:
android:textAllCaps="true"
The fromHtml method is extremely limited in terms of the HTML tags that it supports, and font is not one of them. See http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html for an unofficial list. I did some research on this myself, and I found that fromHtml is based on an obscure and poorly documented rendering engine.
I use this code
Html.fromHtml(convertToHtml("<font color='#145A14'>text</font>"));
public String convertToHtml(String htmlString) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<![CDATA[");
stringBuilder.append(htmlString);
stringBuilder.append("]]>");
return stringBuilder.toString();
}
That looks like a very dark color, are you sure that your screen is capable to display such colors, so you can distinguish them from black? The code snippet looks good, I've tried similar code many times and it worked like a charm. Try it with somewhat brighter, i.e. #ff0000 (red), to verify that it works:
TextView text = ... // find or instantinate your text view.
text.setText(Html.fromHtml("<font color='#ff0000'>text</font>"));
textView.setText(Html.fromHtml("<font color='blue'>text</font>"));
txt_description1.setText(Html.fromHtml("<font color='rgb'>"+str_description1+"</font>"));
If you do not want a single static color and want to directly reflect from editor you can use "rgb". It will reflect the exact color what you have set in editor, just set in textview and concat it with textview value.
And you are all set to go.
Make sure your RGB value is CAPITALIZED. Android can understand #00FF00 but not #00ff00.
try this and it should works
textView.setText(Html.fromHtml("<font color=\"#145A14\">text</font>"));
Yeah I agree, it doesn't work sometimes.
As an alternative, I use in xml for Textview:
android:textColorLink="yourColor"
works like a charm ;)
For color text with hyperlink URL:
textView.setText(Html.fromHtml("You agree to our <font color='#F20000'><a href='https://www.yoururl.com'> Terms of Service</a></font> and <font color='#F20000'><a href='https://www.yoururl.com'>Privacy Policy</a></font>", Html.FROM_HTML_MODE_LEGACY), TextView.BufferType.SPANNABLE);
This worked for me and I am sure it will also work for all.
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