I am setting String in WebView in Android, but i am getting such characters http://img4.imageshack.us/img4/5200/hkgm.jpg. How to avoid them ? IF i put a log and see then it will be proper but only in WebView it is not working properly
Thanks in advance
Code :
String text = "<html><body style=\"text-align:justify\"> %s </body></Html>";
String summary = rssStr.get(position).getEncodedContent();
Log.d("String", summary);
web.loadData(String.format(text, summary), "text/html", "utf-8");
By setting the Encoding type in the Webview can resolve this issue,
wv.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null);
Related
Here i am trying to load Html code as string in webview's loadData() .Nothing is happen over this mehtod but same method is working like charm in below sdk 29.
webview.loadData(html_code,"text/html",null);
Note : Here i am not performing any encoding or decoding operation on string.I am simply passing it as string to above method.
Use this code, it will work.
String newhtml_code = Base64.encodeToString(html_code.getBytes(), Base64.NO_PADDING);
testWebView.loadData(newhtml_code,"text/html", "base64");
Try calling
String encodedHtml = Base64.encodeToString(html_code.getBytes(), Base64.NO_PADDING);
webview.getSettings().setJavaScriptEnabled(true);
before
webview.loadData(encodedHtml , "text/html", "base64");
like below
String html_code= "<html><body>Your Actualtext.</body></html>";
String encodedHtml = Base64.encodeToString(html_code.getBytes(), Base64.NO_PADDING);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadData(encodedHtml , "text/html", "base64");
for more details refer to this link
manifest file in
android:usesCleartextTraffic="true"
and
WebSettings settings = wb_webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(true);
settings.setBuiltInZoomControls(true);
String html_code = "html code";
wb_webview.loadData(Base64.encodeToString(html_code.getBytes(), Base64.NO_PADDING) , "text/html", "base64");
Now it is working after performing base-64 encoding to string html_code.
Issue resolved by passing html_code string as per given instruction in docs
I came up with another solution by using loadDataWithBaseURL
e.g.
webView.loadDataWithBaseURL(null, html, "text/html", null, null)
It should use less CPU and memory resource as no need Base64 calculation and storing.
I facing the same issue and fix it by using loadDataWithBaseURL() instead loadData() method
mWebView.loadData(mHtml, "text/html", "UTF-8");
solution:
mWebView.loadDataWithBaseURL(null,mHtml,"text/html", "UTF-8", null);
The string contains the html page code. As the requirement, it better not to store in the file. How to represent it in the Webview?
Hi if you string contains the html page code then you can directly load in Webview like below.
webView.loadData(webData, "text/html; charset=UTF-8", null);
Try this:
WebView wv = (WebView)this.findViewById(R.id.myWebView);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL(null, /*Your HTML string*/, "text/html", "utf-8", null);
Check out this: http://developer.android.com/reference/android/webkit/WebView.html
String summary = "<html><body>Your <b>HTML</b> code.</body></html>";
webview.loadData(summary, "text/html", null);
for more reference check this answer..
https://stackoverflow.com/a/14002475/5362535
i have a webview which is gets data form the previous page like this
country = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\"><style type=\"text/css\">p{text-align:justify;font-size:125%;}</style></head><body>" + "<p>" + i.getStringExtra("country").toString()+"</p>"+"</body></html>";
when i try to set that to a webview
web.loadData(country, "text/html", "UTF-8");
it shows plain text
when i try the following
web.loadData( URLEncoder.encode(country).replaceAll("\\+", " "), "text/html", Encoding.UTF_8.toString());
it just shows
< and nothing more
Try this:
loadData(country, "text/html; charset=UTF-8", null);
Found in:
Android. WebView and loadData
I am new in android so please help me. my question is
I want to draw hindi text on WebView in android, I am using following code
<string name="hindi_content">विज्ञान के इस युग में मानव को जहां कुछ वरदान मिले है </string>
WebView wv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String s = getResources().getString(R.string.hindi_content);
wv = (WebView)findViewById(R.id.webView);
wv.loadData(s, "text/html", null);
}
but it displaying the special characters. so please help me.
Use the following to load हिन्दी:
wv.loadData(s, "text/html; charset=utf-8", "utf-8");
This happens because of a bug with the encoding parameter of loadData in most Android versions. This parameter is ignored for some reason so the UTF-8 based hindi characters will not be rendered. To fix this you can use one of the following alternatives.
webview1.loadData(hindi_content, "text/html; charset=UTF-8", null);
webview1.loadDataWithBaseURL(null, hindi_content, "text/html", "utf-8", null);
While using WebView, we usually put a URL for it:
WebView.loadUrl(myURL);
but it is possible to put HTML code directly?? So that it will be in a logic that:
WebView.loadContent ( <html><head><script></script></head><body>....</body></html> );
Thanks.
Check out this:
http://developer.android.com/reference/android/webkit/WebView.html
// OR, you can also load from an HTML string:
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
String yourhtmlpage = "<html><body>You scored <b>hello world</b> points.</body></html>";
webview.loadDataWithBaseURL(null, yourhtmlpage, "text/html", "UTF-8", null);
Try this code. It works for me.
WebSettings settings = mDesc.getSettings();
settings.setDefaultTextEncodingName("utf-8");
mDesc.loadData(mDescText, "text/html; charset=utf-8",null);