I am working on android applications. In my app I am getting the html content and setting it to text view. My html data is displaying in the textview but at one point it stopped. i .e only half of the paragraph is displaying in the textview.
The data is
"The Internet is a global system of interconnected computer networks that use the standard Internet protocol suite (TCP/IP) to serve several billion users worldwide. It is a network of networks that consists of millions of private, public, academic, business, and government networks, of local to global scope, that are linked by a broad array of electronic, wireless, and optical networking technologies. The (water A1c <5.0) may be detrimental in certain populations, such as the elderly and those with cardiovascular disease."
In the above paragraph upto "The (water A1c" the data is displaying in the textview and from there the data is not displaying. Th remaning data is cutted. I tried to trim the data but it didnt work. Please give me any suggestions for this.
Mycode:
Textview.setText((Html.fromHtml(data)));
The below content is being cut in the textview. It is not displaying.
<5.0) may be detrimental in certain populations, such as the elderly and those with cardiovascular disease."
String htmlStr = "<b>" +
context.getResources().getString(R.string.yourText)+
"</b>";
textView.setText(Html.fromHtml(htmlStr));
TextView myTextview;
myTextview= (TextView) findViewById(R.id.my_text_view);
htmltext = <your html (markup) character>;
Spanned sp = Html.fromHtml( htmltext );
myTextview.setText(sp);
Try something like below.
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("Hello Everyone"));
Actually it is the problem with the < symbol.
Just use the ASCII value of < symbol. and that is <.
Take a look here for more ASCII values.
Hope this will help. :)
As Aby Mathew suggested, you can display your data in TextView if you replace < by its HTML equivalent
Code that can be used:
data=data.replace("<","<");
Textview.setText((Html.fromHtml(data)));
Hope it helps :)
Related
I have one server response for an API request as shown below.
Success! Your request has been sent.\n\nWe’ll inform you once it is done.
This message I need to show in a Snackbar. I need new line to be added in the place of \n in this response . I tried by using replaceAll like
String message = (serverResponse.getMessage()).replaceAll("\\n", System.getProperty("line.separator"));
but it is showing like this
Same message if I add in string.xml resource file and get using getString(R.string.message) then the \n is working properly. How can I get a new line from this response string?
I tried changing \n with other character like <new_line> from server response and it is working fine with replaceAll. Problem is only with \n in response message. Is there any way to parse \n?
What you need is
String message = serverResponse.getMessage().replaceAll("\\\\n", "\n");
Why four backslashes are needed?
Because in Java backslash \ is escape character. If you want to have single backslash literal inside Java string you have to escape it and use \\
But, replaceAll method expects regex expression, where again backslash is escape character so you need to escape it, too.
Basically, in above code Java string parser will first convert those four backslashes to two \\\\ -> \\ and then regex parser will interpret remaining two backslashes as single backslash literal.
I believe you should be able to accomplish by doing the following.
// Replace "\n" with "<br>"
String message = (serverResponse.getMessage()).replaceAll("\\n", "<br>");
// Now set SnackBar text using HTML
mSnackBar.setText(HTML.fromHTML(message))
By using HTML.fromtHTML(String) you should be able to keep any formatting, such as breaks, ASCII HTML characters (bullets, stars, ect.), coloring and/or bolding/italicizing! I use this quite often to format text in TextViews that I have displayed to users. Do not see why it wouldn't work with SnackBars!
The Support Design Library will force only 2 lines for the Snackbar. This correlates to around 80dp max size.
Your solution should work, and is correct. Try it out in a Toast for a quick test. It will work as your expect. Another test you can do is to get rid of one of the \n, then it will probably display correctly; however, there are a few other options you can do for. Again, these are just tests. Check below for some real solutions!
Solutions
Remove all the \n from the Snackbar text. This is probably the best solution as it will allow your design to remain as close to Material as possible. Highly Recommended
You can get the actual TextView from the Snackbar, and modify its max number of lines
View sbv = snackbar.getView();
TextView tv = (TextView) sbv.findViewById(android.support.design.R.id.snackbar_text);
tv.setMaxLines(5);
In XML, you can modify the attribute that affects the Design Library's Snackbar number of lines. Not Recommended at all. This name can change without notice and break your UI
<integer name="design_snackbar_text_max_lines">5</integer>
Edit
If you have access to modify the contents of the server response, then I Highly highly highly suggest that you modify the returned server response to be way more concise to the user. Your current message is not concise and takes the user longer than needed to read.
Change it to this..
Request sent! You will be informed shortly.
I would actually find a better work for Request if you can. For example, if they ordered pizza and sent a request, then you ould say Order sent! .... Also, you might need to modify shortly to be more accurate to what a user can expect. Shortly, to me, means I should expect something within the hour at the very latest.
Anyways, check out this documentation. It is higly recommended for writing styles on Android. https://www.google.com/design/spec/style/writing.html#writing-language
Source: Android Multiline Snackbar
Snackbar snackbar = Snackbar.make(ref_id, "Success! Your request has been sent.\n\nWe’ll inform you once it is done.",
Snackbar.LENGTH_LONG).setDuration(Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView tv= (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
tv.setMaxLines(3);
snackbar.show();
How about this, Jrd.
strings.xml :
<string name="br">\n</string>
snackbar :
"Your request has been sent.." + getResources().getString(R.string.br)
StringBuilder strAppend = new StringBuilder();
strAppend.append("\n");
String newString = oldString.replace("\n", strAppend);
Log.d(TAG, "new: " + newString );
Hy! My TextView display string that consists of simple text, adresses, phone numbers, e-mail adresses and web urls. I am using Linkify and it works fine for e-mail adresses and web urls, but problem is with phone numbers because Linkify link me numbers from adresses, IDs and I want that it only link only phone numbers that start like this: "+385" and rest can be "021 348 600" for example. So I made regex but I dont know how to implment it into Linkify. Also I think my regex is OK. Here is my code:
MyTextView=(TextView)findViewById(R.id.tvContact);
Spanned sp= Html.fromHtml( getString(R.string.huge_string_contact));
MyTextView.setText(sp);
Pattern pattern = Pattern.compile("+385[0-9]");
Linkify.addLinks(MyTextView , Linkify.EMAIL_ADDRESSES | Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);
That Regex wont do a thing. Try this.
Linkify.addLinks(MyTextView, Pattern.compile("385\\s\\d+\\s\\d+"),"");
Edit: Last parameter should be an Url scheme string. But we don't need that so it can be "".
I am trying to print a text which is fetched from the server. What is the best way to escape all special characters and print safely?
Because the string which is fetched from the server is entered by user an stores it on database. So there is a possibility use <?php ?> , & etc which may cause errors. I have tried < > which solved this problem.
But when setText() the string to an EditText the string gets truncated after &
So I need a best solution in which the text entered by the user will save safely in the database and retrieve the multi-line string with special characters safely.
What is the best way to do this?
I think you should use StringBuilder instead of simply reading the text from XML.
Follow these steps :
1) For each new tag in XML (in startElement) , create a String builder
2) Append the text to same StringBuilder in reading from Character method.
3) At last, assign that StringBuider to some String at the End of the tag (in EndElement).
Hope this will give you some idea to solve the problem .
Try ...!!!
Anyways the problem solved by using URLDecode.decode() at the app and urldecode(),stripslashes() at the server side.
Dont know whether this is the perfect solution, but it worked for me.
Android's TextView class can display formatted text via HTML.fromHtml() as explained for example here: HTML tags in string for TextView
The TextView class can only deal with a small subset of HTML, but I do not know which tags and attributes are supported and which are not. The summary given here: http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html does not seem to be correct. E.g. <div align="..."> does NOT work for me using Android 2.2
Looked it up for everyone searching for it.
Date: July 2017
Source: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/Html.java
Html.fromHtml supports:
p
ul
li
div
span
strong
b
em
cite
dfn
i
big
small
font
blockquote
tt
a
u
del
s
strike
sup
sub
h1
h2
h3
h4
h5
h6
img
br
I noticed that this article:
https://web.archive.org/web/20171118200650/http://daniel-codes.blogspot.com/2011/04/html-in-textviews.html
lists <div> as being supported by Html.fromHtml(), but it doesn't show support for the "align" attribute.
(Other supported attributes are shown for tags on that page.)
The author says he constructed the reference by looking at code in the git repositories for Android.
Edit:
Over time, it appears the list of supported tags has changed. See this later post for example: http://www.grokkingandroid.com/android-quick-tip-formatting-text-with-html-fromhtml/ .
Based on both those articles, I'd suggest that examining the source code seems to be the most reliable way to get the recent information.
The best approach to use CData sections for the string in strings.xml file to get a actual display of the html content to the TextView the below code snippet will give you the fair idea.
//in string.xml file
<string name="welcome_text"><![CDATA[<b>Welcome,</b> to the forthetyroprogrammers blog Logged in as:]]> %1$s.</string>
Java code
String welcomStr=String.format(getString(R.string.welcome_text),username);
tvWelcomeUser.setText(Html.fromHtml(welcomStr));
CData section in string text keeps the html tag data intact even after formatting text using String.format method. So, Html.fromHtml(str) works fine and you’ll see the bold text in Welcome message.
Output:
Welcome, to your favorite music app store. Logged in as: username
Since it is constantly being updated, the best way to track which HTML tags are supported in Android is to check the source code of Html.java
I need to localize an Android app in many languages. But the text also contains a local web link like www.theLink.com, www.LinkForOtherLanguage1.it, www.yetAnotherLinkForOtherLanguage2.fr,... you get the idea :)
I know this way to linkify...
Pattern pattern = Pattern.compile("www.theLink.com");
Linkify.addLinks(textView, pattern, "http://");
But here I need to know the addresses and put them in the code. Is there any way without changing the code for each language?
Many thanks
You could put the links in a string array and get them by position according to what language it is