Line break \n doesn't work, it show as string - android

I have a String with (\n) as a line break
String mytext = "<p>Your email is not correct.\nPlease check again</p>"
//(mytext value load from my database)
And i put mytext on webview like this :
web_view.loadDataWithBaseURL("", mytext, "text/html", "UTF-8", "");
But, i don't know why \n doesn't work as line break, but it show as String "\n"?
How to fix it?
I tried to replace \n with </ br> with mytext.replace("\n","</ br>") and mytext.replaceAll("\n", "</ br>") but it doesn't work.
Thanks
Updated
So the solution is :
mytext.replace ("\\r\\n", "<br>").replace ("\\n", "<br>");
Thank you, #ChristianJonassen for your help.

You are using HTML, you need to introduce an HTML line break.
In your case, the source code of the page will be given the line break, but not the representation given to the user.
Here is how you can fix it:
String mytext = "<p>Your email is not correct.<br />Please check again</p>"
Since it comes from an external service, you can edit the String by replacing \n:
mytext.replaceAll("\n", "<br />");

Related

Inserting HTML formatted (not a resource) string in android TextView

I need to plug an HTML formatted string into a TextView. I'm retrieving the text from a database and when I insert it using this code
WebView webview = (WebView) view.findViewById(R.id.webView);
text = "<html><body>" + {text retrieved form database} + "</body></html>"
webview.loadData(text, "text/html", "utf-8");
It works but this code
TextView output = (TextView) view.findViewById(R.id.simple_text);
output.setText(Html.fromHtml(text));
strips out all HTML formatting.
What is the right way to do this?
Start by getting rid of the <html><body> and </body></html> parts, as Html.fromHtml() does not use them.
Then, limit your HTML to use tags that are supported by Html.fromHtml().

How do i display html Special Characters in my android textview

Actually i am retrieving text from json web service which is HTML text containing tags and all that ,which i am executing with Html.fromHtml ,but the problem is that the Special Characters are displayed as diamond with question mark which i am not able to display in its correct form.
below is the code where i got the problem
Description=Description.replaceAll("\\<.*?>","");
Description=Description.replaceAll("\\“", "");
Description=Description.replaceAll("\\”", "");
// Description=Description.replaceAll("♦", "");
//Description=Description.replace("“", "");
details.setText(Html.fromHtml(Description).toString());
Description is the String variable where i have stored the html text,i have googled alot but doesn't get anything.hope i got something from here
thanks in advance
In my webview I did sothing like this.
productInfoWebview.loadData(value, "text/html; charset=UTF-8", null);
Decoding HTML is decoding HTML entities to Java raw unicode characters.
String html = "B & This is HTML";
String java = Html.fromHtml(html);
> Output: "B \u0026 This is HTML"
String strJava = Html.fromHtml(html).toString();
> Output: "B & This is HTML"

The text from Android EditText becomes garbage characters in WebView

I am going to get the text in an EditText and then display the text in a WebView. The following code works for ASCII characters. For non-ASCII characters, the text in WebView becomes garbage characters.
String input = mEditText.getText().toString();
String html = makeHTML(input); // append HTML elements and headers including MIME and ENCODING header
mWebView.loadData(html, "text/html", "utf-8");
I thought that I was doing something wrong with my HTML, so I try to display the text directly in the WebView without modify the text. However, the result was the same.
String input = mEditText.getText().toString();
mWebView.loadData(input, "text/html", "utf-8");
The makeText() of Toast which displays non-ASCII text in EditText without any problem.
Does anyone know the answer?
WebView might not be able to load certain "unsafe" HTML characters. Try using:
String input = mEditText.getText().toString();
String html = makeHTML(input);
String encodedHtml = URLEncoder.encode(html,"UTF-8");
mWebView.loadData(encodedHtml, "text/html", "utf-8");
The URLEncoder.decode(encodedHtml,"UTF-8") method might also be useful.
Finally, I solve the problem by using loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl) of WebView
mWebView.loadData(data, mimeType, encoding)
mWebView.loadDataWithBaseURL("", data, mimeType, encoding, "")
seems to be same but actually does not.
In my case, loadData() failed to encode the characters properly and failed to load images saved in the asset folder.

Open an URL in Android MediaPlayer

I am trying to open an URL with androids MediaPlayer Class by using:
MediaPlayer.create(pContext, Uri.parse(m_sUrl));
i have allready replaced all the Spaces in m_sUrl String with %20 by using:
m_sUrl = m_sUrl.replace(" ", "%20");
But the MediaPlayer.Create Method return null to me. So there seems still to be something wrong by parsing the m_sUrl String.
Thats the URL string i am trying to stream:
http://www.se.hs-heilbronn.de/~poneu/files/musik/oeffentlich/2007-03-10/01%20-%20L.v.%20Beethoven-%20Ouvertüre%20Nr.%203%20zur%20Oper%20-Leonore-%20op.%2072a.mp3
so as you can see, it seems to be the ü character. Anyone know what i have to use in an valid url for ä ö ü ß and so on?
Use URLEncoder and you have to keep in mind to encode only unsafe character:
String query = URLEncoder.encode("depeche mode", "utf-8");
String url = "http://stackoverflow.com/search?q=" + query;
in the exemple that i mentioned below the unsafe character is the space that will be transformed to %20
hope this will help
use URLEncoder instead of manually replacing instances of single characters.

Android WebView not loading local html string correctly

I'm working on a project where I'm receiving some html string which I should show in WebView. The problem is that the html string contains only body part, without any tags like <html>, <head>, <body> and when I'm trying to show the received string in webView the result is showing the tags included in html string received from server.
Here is some example what I receive and what I'm doing :
Html String received from server :
05-30 14:02:56.785: D/(16970): body : <p align='justify'>The so-called Fiscal Compact is unlikely to be approved by the Latvian parliament in the second and final reading as four opposition MPs said that they would not back the initiative, local media reported on Tuesday evening. The second reading is scheduled for May 31. Under local legislation, the bill should be backed by 67 MPs in the 100-seat parliament, though the ruling coalition controls only 56 seats and depends on the opposition Union of Greens and Farmers (ZZS) to approve it. During the first reading 68 MPs backed the bill. The four MPs from ZZS will meet PM Dombrovskis later today to discuss the issue. They want more clarity about the country’s participation in neighboring Lithuania’s nuclear power plant project. Previously, ZZS said it extends its support on condition that the government fights for higher subsidies for farmers in the 2014-2020 EU budget framework.</p>
Here is my code :
String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
Log.d("","body : "+bodyTxt);
String newBody = "<html><body>" + bodyTxt + "</body></html>";
body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);
and the result is this :
I've tried using body.loadData(newBody, "text/html", "UTF-8");
, but this way didn't even show me the result.
So any ideas which I'm doing wrong and how can I fix this?
Thanks in advance!
As I can see from your logs you are receiving the html tags not with < symbol,but like >. I guess if you replace these characters in your html string it will work as it should be. Try this :
String bodyTxt = cursor.getString(cursor.getColumnIndex("body"));
String replaced = bodyTxt.replace("<","<");
Log.d("","replaced : "+replaced);
String replaced2 = replaced.replace(">", ">");
Log.d("","replaced2 : "+replaced2);
Log.d("","body : "+bodyTxt);
String newBody = "<html><body>" + replaced2 + "</body></html>";
body.loadDataWithBaseURL(null, newBody, "text/html", "utf-8", null);
you can use this
webview.loadData(Html.fromHtml(htmlData),"text/html","utf-8");
This is how i do it;
String htmlContent = "<html><body>hello</body></html>";
WebView wv = (WebView) findViewById(R.id.my_view);
wv.loadData(htmlContent, "text/html", "UTF-8");
Investigate how the data looks in the db, all the "<>" and such are quoted with "<", this might be the problem you looking for.

Categories

Resources