I'm using Wordpress API to get posts in my app. I'm trying to add new line while writing a post. However when I ,for example, add post like this :
THIS IS /n a POST
In my app I can see /n in the textView exactly as I put it above. The same thing is happening when I try to use <br> tag.
Setting post in textView:
String content = String.valueOf(HTML.fromHTML(p.excerpt));
vItem.shortContent.setText(content);
Why is that ?
EDIT!!!
It's working after adding :
String content = p.excerpt.replace("\\n","<br>);
vItem.short_content.setText(Html.fromHtml(content));
Thanks!
Supposing p.excerpt is an HTML String, use
vItem.shortContent.setText(Html.fromHtml(p.excerpt));
instead.
You are seeing the new lines because String.valueOf(Object) calls toString() from the param object. Since Html.fromHtml() returns an Spannable, your code is doing a spannable.toString() so its returning the literal text.
you can also put the html formated text in the string resources directory as follows
<string name="helloworld">
<![CDATA[
This is now formatted:<br />
<strong>Hello world</strong> - this should work fine
]]>
</string>
you can then call the string in your activity as follows
txtView = findViewById(R.id.mytext);
String formatedstring = getString(R.string.helloworld);
Spanned txt = Html.fromHtml(formatedstring);
txtView.setText(txt);
Related
Due to HTML usage within a string resource, I can't convert this to string from a charsequence (I will lose the formatting otherwise).
<string name="exponent_key">x<sup><small>y</small></sup>/string>
After using getString() I want to replace the 'y' with 'other stuff' but how do you do that? It seems like a simple question but for some reason I can't find anything about it.
Edit: Now that I think about it, can I convert the charsequence to a string that contains the HTML code, and then convert it back to a charsequence later?
Edit: Forgot to mention that the string gets set to a button title, and then retrieved (where it is then used).
There is. Create a function where, as a parameter, you take a string that needs to be formatted. And in function, you just take it through itterator, and after that Html.fromHtml()
in your string.xml
<string name="exponent_key">x<sup><small>%1$d</small></sup></string>
in your code
textView.setText(getString(R.string.exponent_key,2))
Let's break down your question in multiple steps:
Replacing the y with "other stuff" can be done like this:
String.format("%1$", "otherStuff");
If you use getString(), you can do the same thing like that:
<string name="exponent_key">%1$</string>
---
String string = getString(R.string.exponent_key, "otherStuff");
For more than one element, do this way:
you can do that like this:
<string name="string_name">%1$ : %2$</string>
---
getString(R.string.string_name, new String[]{"hello", "world"});
In XML you cannot nest HTML code, since HTML is another type of XML and the parser messes up and cannot recognize what tags are for Android and what are for HTML. But.. there's a trick. You can do that in this way:
<string name="exponent_key"><![CDATA[x<sup><small>%1$</small>/sup>]]></string>
So, with the string above in your XML, just use and you're fine:
getString(R.string.exponent_key, "otherStuff");
Note: if you need to show the HTML in a TextView, just use Html.fromHtml:
textView.setText(Html.fromHtml(getString(R.string.exponent_key, "otherStuff")));
Consider the effect you want to achieve.
you can do like this.
SpannableString ss = new SpannableString("2");
// set superscript
ss.setSpan(new SuperscriptSpan(),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// set font size
ss.setSpan(new AbsoluteSizeSpan(12,true),0,ss.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.append(ss);
I'm trying to get back the text of a Button as a String, I set it previously:
button.setText(Html.fromHtml("<sup><small>1</small></sup>/<sub><small>8</small></sub>"));
but, the String returned from Html.toHtml() is:
<sup>1</sup>/<sub>8</sub>
How do I get the tag back?
To get the string:
SpannedString spannedString = new SpannedString(button.getText());
Html.toHtml(spannedString)
toHtml() does not handle <small> tags, nor any other tags that fromHtml() turns into RelativeSizeSpan objects. You would have to write your own Spanned-to-HTML conveter.
TextView tv = (TextView) findViewById(R.id.text);
tv.setText(Html.fromHtml(getResources().getString(R.string.test)));
Define string 'test' in strings.xml
<string name="test"><![CDATA[<sup><small>1</small></sup>/<sub><small>8</small></sub>]]></string>
In your case simple put button.setText(Html.fromHtml(getResources().getString(R.string.test))); It worked for me.
The response of a web service is the following :
<span style="color:#DDFFEE;">You have to pay you bil before <b>12 june 2012</b> for your informations.</span></br></br> You have already a large time.
I want to display this in a TextView.
I can't just simply put the response below in a String and applicate
myTextView.setText(Html.fromHtml(response));
It doesn't work because it can't be a String.
How can I do this dynamically please ?
You just need to put the String you get as a response, inside the fromHtml call, if it's not a String, maybe you just need to append .toString() to it, to get a String..
textView.setText(Html.fromHtml("<h2>This is HTML</h2>"));
You need to use Html.fromHtml() to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.
myTextView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
here : Similar question
I have a String , this string may contain any value(i mean it may include HTML tags).
i created a TextView in java and now i need to set this text to textview.
Generally
String str;
Textview tv = new TextView(this);
tv.setText(str);
but if the str = < b &rt this is testing < /b &rt
then in this case i have to set the text for the textview with Bold (need to read HTML tags and assign according to that).
are there any API's available in java to read HTML and how can i proceed on this..
thanks,
Do like this.
String htmlStr = "< b &rt this is testing < /b &rt";
tv.setText(Html.fromHtml(htmlStr));
Method Html.fromHtml(String) -
Returns displayable styled text from the provided HTML string. Any tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.
This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.
You can do this:
String myHtml = "< b &rt <b> this is testing </b> < /b &rt";
myTextView.setText(Html.fromHtml(myHtml));
You can put your html content to strings.xml and refer it form there. If you have large html content you can put the content in strings.xml between ![CDATA[ and ]].
In your strings.xml add
<string name="myhtml><![CDATA[<html> your html conten </html> ]]> </string>
In your activity
Textview tv= new TextView(this);
tv.setText(Html.fromHtml(getString(R.string.myhtml));
I am receiving a webservice strings in html code, and needed to put them in this html code to put a TextView.
Example:
String str = "<b>Hello</b>";
// my attempt
textView.setText(Html.fromHtml(str));
// this only convert to <b>Hello</b> instead of running the html code.
Well that's an ugly trick but it works:
textView.setText(Html.fromHtml(Html.fromHtml(str).toString()));