I am loading html pages from assets and showing in application. HTML pages are showing but when I tried to change background color of button on "onclick" then background color is not getting change.
If i test pages with browser then it's working but when I load html pages in android using webview then it's not working.
onclick function getting called but somehow style property not getting applied.
I tried following three ways.
1. $('#btnAC').css({'background-image', 'linear-gradient(180deg, black, red) !important'});
2. document.getElementById("btnAC").style = "background-image: linear-gradient(180deg, black, red) !important;";
3. $('#btnAC').css('background-image', 'linear-gradient(180deg, black, red) !important');
Java code :
WebView webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("http:///android_asset/Web/index.html");
You could use WebView.loadDataWithBaseURL
htmlData = "" + htmlData;
// lets assume we have /assets/style.css file
webView.loadDataWithBaseURL("file:///android_asset/", htmlData, "text/html", "UTF-8", null);*
Related
I am using the following code for rendering a dynamically created html string on a webview. It works properly on my device. But on one of the user's device, the webview displays the raw html tags and content instead of rendering properly.
I cannot reproduce the issue on my devices. What can be cause for this issue to happen. Any device level settings? or Encoding related issue?
String htmlContent = "<html><body> .... </body></html>";
webview.loadDataWithBaseURL(null, htmlContent, "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);
I'm retrieving Accelerated Mobile Pages (APM) formatted HTML form a webcall. I'm then trying to display that AMP in a WebView. So I'm setting up my WebView like this:
webView = (WebView) findViewById(R.id.web_view);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
String content = article.getContentAmp();
webView.loadData(content, "text/html; charset=utf-8", null);
It displays content without error, but it's not displaying correctly. It ignores for example amp-youtube elements and embeded tweets.
Is there something else I have to do with the WebView or the WebChromeClient to display AMP correctly?
EDIT:
The answer below works. But in my specific case, I was using the WebView inside a NestedScrollView. Which worked fine in Android 5.0+, but below that, the amp- elements would not load.
Taking the WebView out of the NestedScrollView solved the problem. It's parent is now a relative layout.
So if you're having the same problem, try simplifying your layout so that the WebView is not deeply nested. And definitely don't put your WebView inside a NestedScrollView.
You cannot display elements such as embedded youtube videos, images, tweets etc without giving the WebView a base url. The solution was to use webview.loadDataWithBaseUrl instead on webView.loadData. You must pass it a valid url.
webView = (WebView) findViewById(R.id.web_view);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
String content = article.getContentAmp();
String baseUrl = extractUrlFromContent(content);
webView.loadDataWithBaseURL(baseUrl, content, "text/html", "utf-8", "");
I just suffered from a case that some local html file will become blank page after reload/ being ignored for some period of time. However, the html file will works fine again in a new tab.
It is remarkable that the page contains no Javascript.
Besides, it is also reported that the same set of local html will become blank page rarely and randomly when loading in Android Webview. The permission configuration and folder path (using:file:/// as the beginning of the url) are both valid and exists in the machine. The Webview has set clearcache to true before each loading.
I have also checked the page that, all the page elements are still there but they cannot be displayed.
It is remarkable that, reload the webpage will never work.
Here is the capture of the page after it become blank page.
Any idea?
EDIT 1
source code of Android loading the local html file
WebView wv = new WebView(this);
wv.setPadding(40, 40, 0, 0);
wv.setWebChromeClient(new MyWebChromeClient());
wv.clearCache(true);
WebSettings webSettings = wv.getSettings();
webSettings.setTextSize(WebSettings.TextSize.NORMAL);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
File ext = Environment.getExternalStorageDirectory();
String filePath = ext.toString() + "/some_folder/";
wv.loadUrl("file:///" + filePath + "HTML Forms/form1.html");
vh.webLinearLayout.addView(wv,
new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
I am trying to make an Android app display a website in a WebView, but only the website home page actually show content properly via the WebView, the other pages completely disregard the website styles and display content with a white background and blue default hyperlinks. I tested the same type of app on iOS and it works fine there. What can I do about it?
This is the method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.awesomestories.com");
}
Settings should be set. Tricks Like this:
WebSettings settings = webview.getSettings();
settings.setBuiltInZoomControls(true);
settings.setPluginState(PluginState.ON);
settings.setJavaScriptEnabled(true);
settings.setSupportZoom(true);
If you have kept Single Column Setting in WebView remove it, it will work just perfect.
myWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
If you have not kept Single Column setting and still its not working, then check that you have enabled JavaScript or not, if not then enable it.
myWebView.getSettings().setJavaScriptEnabled(true);
Android WebView requires you to explicitly enable javascript.
Styles in the page may be loaded via javascript, and generally websites don't tend to work good without it, so enabling it is important and might solve your problem in addition to that.
myWebView.getSettings().setJavaScriptEnabled(true);
i am receiving html from server and need to show it to user with simple styles (like bold, italic etc) and user should be able to edit it and impart similar style in it. i used webview, but it is not editable. I have seen some prev ques about rich text editor, but none of them are clear about what to do, or i could not understand as i am a newbie in this field. Thanks in advance. Here data is a string which is recieved from server, it contains html tags within. I need an editor which can add new text, edit prev one, bold/unbold selected text and some similar functions
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setEnabled(true);
webView.loadDataWithBaseURL("file:///android_asset/", data, "text/html",
"utf-8", null);