<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.
Related
I try to use html-tags for my text in EditText and I want to save it in Room database. So I created a string with html tags like that:
EditText.setText("Hi <font color="#4B0082">there</font>".parseAsHtml())
It works, but when I try to get it back from the EditText like that:
EditText.text.toHtml()
I get this:
<p dir="ltr">Hi <span style="color:#4B0082;">there</span></p>
Why EditText changes my tags and adds something else? I don't need "p" and "span" tags at all. What am I supposed to do? How to get my original string back from the EditText?
Android views, in general, do not operate directly on HTML tags. When you set the text to the EditText, a translation occurs to Spans. In your example,
"Hi <font color="#4B0082">there</font>"
the font color is translated to a ForegroundColorSpan.
When you try to get the text back from the EditText, a reverse translation occurs and the ForegroundColorSpan is translated to an HTML span tag with a style that specifies the font color. This is equivalent in appearance to the original HTML code.
I assume, since your are placing the text into an EditText, that the text could be changed and what you want is the updated text encoded into the same type of HTML that you placed into the EditText. (If you simply want to retrieve exactly the same string back that you put into the EditText you could try using a View tag).
I think that the only way to get the translation you want is to write your own conversion routine. You can get the spans from the text with getSpans(). You would write something similar to Html.toHtml(). This could be a simple or hard task depending upon how robust the translation needs to be.
There could also be a library that can be customized, but I am unaware of one.
Try this one:
String text = Html.toHtml(yourEditText.getText())
It should return you a text with html formatting
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!
Can I span a textview to make some part bold in the layout itself. I have around 20 xml files. I am not using any activity for them, so I cannot do it programatically. So is there a way to specify it in xml layout itself. I have already tried using between them but it doesn't works.
android:text="Lets look how to make <b> negative sentences <b> in simple present tense."
doesn't work.
You can style a textView in xml but I believe it has to be a string resource rather than a hardcoded value like you are doing. Move the string to strings.xml and access it using resource id
android:text="#string/name"
Check this post.
Quoted from the post:
Supported HTML elements include:
<b> for bold text.
<i> for italic text.
<u> for underline text.
Check here
http://developer.android.com/reference/android/text/Spannable.html
or
https://sites.google.com/site/androidhowto/style-text
or
http://www.java2s.com/Code/Android/2D-Graphics/UsingSpannabletextandsetstyle.htm
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.
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