How can I read special character in webview? - android

I am developing an article managing android application, where articles are parsed through JSON.
All the values that are parsed by JSON are loaded in web view, but there are some special character that are not read by WebView.
{
web1 = (WebView) findViewById(R.id.webView2);
web1.setWebViewClient(new myWebClient());
web1.getSettings().setJavaScriptEnabled(true);
web1.loadData(c,"text/html", "UTF-32");
}

you can do like this way,
WebView webview = new WebView(collection.getContext());
webview.loadDataWithBaseURL(url, textWithQuotesIn, "text/html", "UTF-8", url);

Related

Twitter video not loading android webview

When playing twitter video in android web view it shows the message 'by playing this video you agree to the Twitter use of cookies' and video is not played after that.
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
} else
CookieManager.getInstance().setAcceptCookie(true);
}
webView.getSettings().setJavaScriptEnabled(true);
webView.setBackgroundColor(Color.parseColor("#ffffff"));
webView.loadDataWithBaseURL("file:///android_asset/", htmlTemplate, "text/html", "UTF-8", "");
JavaScript is disabled in a WebView by default.
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
UPDATED:
Use TweetView for Native Videos
Launching native video support for Twitter Kit
Call loadDataWithBaseURL instead of loadData, like this:
loadDataWithBaseURL("https://mywebsite.com", mContent, "text/html", "UTF-8", null);

Android Studio Assets file cannot be accessed in WebView

I have been trying to figure out almost 2 weeks.
I have read previous posts and tried the proposed solutions but they didn't work for me.
Iam using Android Studio, I need Webview to access a local Jpg stored in Android Assets file.
Here is my code:
webView = (WebView) findViewById(R.id.webview);
startWebView("file:///android_asset/railchinese.jpg");
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
private void startWebView(String url) {
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
Here is the Assets location screen:
Kindlly let me know where is the mistake?..
Try this
String htmlData="<img src= railchinese.png>";
webView.loadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "utf-8", null);

Android: How to convert local string(html) to html page and display in the Webview

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

How to pass html string to webview on android?

I am parsing xml and then loading it to web view. After parsing, I am creating four strings so that I could append all strings to one view. I am able to get two views on the web view, but not the first two strings.
Please suggest in my code, where I am going wrong and the correct way to get the formatted html strings on the web view.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String chapterTitle = "";
String SubChapterTitle="";
String chapterIntro ="";
String chapterContent="";
View view = convertView;
if (convertView == null) {
// view = inflater.inflate(resourceid, null);
view = getLayoutInflater().inflate(R.layout.webviewitem, null);
}
synchronized (view) {
WebView wv = (WebView) view.findViewById(R.id.contentWebView);
WebSettings settings = wv.getSettings();
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
settings.setJavaScriptEnabled(true);
settings.setDefaultZoom(ZoomDensity.FAR);
// wv.setBackgroundColor(0);
wv.setVerticalScrollBarEnabled(false);
wv.setHorizontalScrollBarEnabled(false);
/*String txtChapTitle = Intro.book.getsecretList().get(position)
.getChtitle().toString();*/
if (!(Intro.book.getsecretList().get(position).getChtitle()
.toString().equals(""))){
chapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position)
.getChtitle().toString()+"</font></b>";
}
if (!(Intro.book.getsecretList().get(position)
.getSubtitle() == null)) {
SubChapterTitle = "<b><fontSize=4>"+Intro.book.getsecretList().get(position)
.getSubtitle().toString()+"</font></b>";
}
if (!(Intro.book.getsecretList().get(position)
.getIntro() == null)) {
chapterIntro = "<b><fontSize=2>"+Intro.book.getsecretList().get(position)
.getIntro().toString()+"</font></b>";
}
if (!(Intro.book.getsecretList().get(position)
.getContent() == null)) {
chapterContent = "<fontSize=2>"+Intro.book.getsecretList().get(position)
.getContent().toString()+"</font>";
}
StringBuilder content = new StringBuilder();
content.append(chapterTitle+SubChapterTitle+chapterIntro+chapterContent);
JsInterface Jsi = new JsInterface();
Jsi.wordDef = content ;
Log.v("Content", "" +content);
wv.addJavascriptInterface(Jsi, "interfaces");
wv.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
view.setHapticFeedbackEnabled(false);
}
});
wv.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onJsAlert(WebView view, String url,
String message, JsResult result) {
return super.onJsAlert(view, url, message, result);
}
});
wv.loadUrl("file:///android_asset/wordview.html");
}
return view;
}
}
I am able to get chapterIntro and chaptercontent on the web view, but not the first two strings.
i have successfully done by below line
//data == html data which you want to load
String data = "Your data which you want to load";
WebView webview = (WebView)this.findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadData(data, "text/html; charset=utf-8", "UTF-8");
Or You can try
webview.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
To load your data in WebView. Call loadData() method of WebView
webView.loadData(yourData, "text/html; charset=utf-8", "UTF-8");
You can check this example
http://developer.android.com/reference/android/webkit/WebView.html
Passing null would be better. The full codes is like:
WebView wv = (WebView)this.findViewById(R.id.myWebView);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadDataWithBaseURL(null, "<html>...</html>", "text/html", "utf-8", null);
I was using some buttons with some events, converted image file coming from server.
Loading normal data wasn't working for me, converting into Base64 working just fine.
String unencodedHtml ="<html><body>'%28' is the code for '('</body></html>";
tring encodedHtml = Base64.encodeToString(unencodedHtml.getBytes(), Base64.NO_PADDING);
webView.loadData(encodedHtml, "text/html", "base64");
Find details on WebView
After struggling the 2 hours of effort I found the solution
I want to run this code in webview
"<div style='width:100%;height:0px;position:relative;padding-bottom:56.250%;background:#000;'><iframe src='https://www.scorebat.com/embed/v/N09DWllRdlNraERnSXRRa1VJMTF4Zz09/?token=MTkxMTdfMTY1MjEyMzY2MF9kNzRkZTI4MTc3ZDAxZTNkOTQyOTg1OWE3MTcyYmFhY2EyYTI2YTZh&utm_source=api&utm_medium=video&utm_campaign=apifd' frameborder='0' width='100%' height='100%' allowfullscreen allow='autoplay; fullscreen' style='width:100%;height:100%;position:absolute;left:0px;top:0px;overflow:hidden;'></iframe></div>"
But I only got the white screen after trying all the answers
Now do this as your code will also work
WebView webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.getSettings().setMediaPlaybackRequiresUserGesture(false);
webview.setWebChromeClient(new WebChromeClient());
webview.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null);
Here webview.loadDataWithBaseURL(null, data, "text/html", "UTF-8", null); data is the HTML and javascript code
Add this line in your application tag as well
android:hardwareAccelerated="true"
I hope it will solve your issue

android/java replace string to bold or some color in webview in html

i have a string ticker..
ticker="hdfc:123:-1.90 tcc:231:+1.3 as continue to hundreds of
ticker will get from the server. i just want to bold the name... so help me to find my solution.....
here is my code...
tiker = TickerConnection.getInstance().Ticker();
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);String summary = "<html><body><MARQUEE>"+tiker+"</MARQUEE></body></html>";
mWebView.loadData(summary, "text/html", "utf-8");
hey following is ur solution... u jst have to add tag <b>
tiker = TickerConnection.getInstance().Ticker();
WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);String summary = "<html><body><MARQUEE><b>"+tiker+"</b></MARQUEE></body></html>";
mWebView.loadData(summary, "text/html", "utf-8");

Categories

Resources