TextView Html formatting - android

I have a textview which should render its content with HTML formatting. From code, this can be achieved like this,
textView.setText(Html.fromHtml(someText));
Is there a way to do this through the XML?
I ask because i do not want to set the text through the code, In my case, the text comes from a DB call and displayed through an adapter.

Yes there are a bunch of (simple) tags that are understood by TextView -- if the text is set in XML from a string resource.
So basically
<TextView text="#string/foo" .. />
It is also possible to give templates like "Hello <b>%s</b>", but here you still need to run some code to fill in the value for %s
Have a look at http://developer.android.com/guide/topics/resources/string-resource.html for formatting hints and short examples.

Related

How to get the same html-string from EditText that i put it there

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

Change font of superscript text

I am writing an view where I need to display superscript data. I am following this to achieve this.
Along this is there any way to change font size(in number) of superscript text.
EDIT
Commanware suggested link work great, except one thing. I need superscript bit above of base text. I'm updating image for same, please refer. I'm using same code with mention in reference code.
Here either can go for separate text view could be second priority solution. Any suggestion !
try this one this is working code:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("04:30<sup><small>pm</small></sup>"));
you cant pass text size in numbers, yo have to change the size to string :
<font size="3" color="red">This is some text!</font>
Use this Code :
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("your Text <sup style="font-size:5(yourfontsize);">subscript</sup>"));
Try this and Let me know :)

Android textview different style for bold text

I have a long string, with multiple bold words inside the string.
The styling of the non bold sentences are done inside the TextView XML part and this is looking fine.
But i want to make the bold words bigger and thicker.
Is this possible to change this with XML?
for example:
<string name="long_text">This should be a <b>long</b> text with different <b>styling</b></string>
The bold words are supposed to be 1.5 times bigger
Or should i do this in the Java code?
yes you have to do this in java because there is no way to as per your requirement,
you can use TextAppearanceSpan in java code with Spannable
if i am getting you right than try the following..
textview.setText(Html.fromHtml(getResources().getString(R.string.long_text)));
Mark as right if it works for you .

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.

Android Listview - html formating of rows

is it possible to format text in rows in Listview using html? I need to use font, b, i tags
I use this code to fill ListView
setListAdapter(new ArrayAdapter<String>(MyActivity.this,R.layout.resultlist,v));
where v is a vector array filled with strings
Thanks
No, but you can use the xml attributes to format the text in a TextView. For example, you can use the android:textStyle attribute to make the text bold, or italic. So if you want bold, you can do android:textStyle="bold" or if you want both bold and italic you can do android:textStyle="bold|italic". If you want different parts of your string formatted differently, checkout this SO Post.
Well, you can use:
Html.fromHtml("text with html");
But, I think it will slow down your ListView. Try to use android styling in your R.layout.resultlist xml layout. I'm sure you can find a way to avoid html formatting.

Categories

Resources