How do I avoid webview zooming and improve its performance? - android

I have a js game code which I am showing in a webview. The code is defined such that it takes up the entire html page template, and when I pull it in a webview, I adjust the size accordingly.However, it zooms on double taps, which makes it difficult to play the game.How do I prevent it from zooming. Also, is there a way to speed up the performance, as the game is extremely slow and not ideal right now.
Here's the code for the webview:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
WebView webview = (WebView)findViewById(R.id.webview);
webview.setInitialScale(1);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webview.setScrollbarFadingEnabled(false);
// webview.getSettings().setRenderPriority(RenderPriority.HIGH);
// webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.loadUrl("file:///android_asset/index.htm");
//webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setSupportZoom(false);
Thanks,
Brian
My HTML Code bit :
<html>
<head>
<meta charset="utf-8">
//meta data tag below
<meta name="viewport" content="width=device-width,... etc>
<title>Dino Dragon </title>
<link rel="stylesheet" href="css/main.css">
</head>
<body>

Have you placed in the beginning of your HTML document the following definition:
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
This could prevent the webview from 'zooming in'.

You might need to extend a customized webview and override the onTouchEvent method.
And in the event callback, call setSupportZoom(false). Something like this:
#Override
public boolean onTouchEvent(MotionEvent ev) {
getSettings().setSupportZoom(false);
return super.onTouchEvent(ev);
}

Use instead of
webView.getSettings().setUseWideViewPort(true); to
webView.getSettings().setUseWideViewPort(false);

Related

Android 7 iframe video 'Refused to display' in WebView

I have and application, which displays html content from other sources. This content may contain iframe embedded videos. For special case of twitter videos the behavior is different and video remains blank after running.
The logcat shows message:
Refused to display 'videoUrlHere' in a frame because an ancestor violates the following Content Security Policy directive: "frame-ancestors *".", source: about:blank (0)
Example url is: https://twitter.com/i/videos/tweet/840948416742117377?embed_source=clientlib&player_id=0&rpc_init=1&language_code=en
The interesting thing is that this approach works on android 5 and lower.
Loading of content
mWebView.loadDataWithBaseURL("file:///android_asset/", mHtmlSource.toString(), "text/html", "utf-8", null);
Initialization of WebView:
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.addJavascriptInterface(new InjectedAppObject(), "InjectedAppObject");
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDisplayZoomControls(false);
webSettings.setLoadsImagesAutomatically(true);
Headers added to html:
<html lang=\"cs\" dir=\"ltr\">
<head>
<meta charset=\"utf-8\">
<meta name=\"keywords\" content=\"\">
<meta name=\"description\" content=\"\">
<meta http-equiv=\"Content-Security-Policy\" content=\"default-src * data: \'unsafe-inline\' \'unsafe-eval\'; frame-ancestors *; \">
<meta name=\"robots\" content=\"index,follow\">
<title>App title</title>
<link href=\"file:///android_asset/html/css/style.css\" rel=\"stylesheet\">
<script src=\"file:///android_asset/html/js/jquery.min.js\"></script>
<script src=\"file:///android_asset/html/js/app.js\"></script>
<script src=\"file:///android_asset/html/js/disposable-iframe.min.js\"></script>
<script src=\"file:///android_asset/html/js/iframe.js\"></script>
</head>

How to inject CSS before show WebView?

I need some help on how to inject CSS before showing WebView. Appreciate any help.
I need to inject CSS into WebView from assets, before the WebView shows.
You should load url contents manually to your html structure. put this code as page.html in your assets folder.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<style type="text/css">
// your css code here
</style>
</head>
<body>
${body}
</body>
</html>
then in your java code:
String pageBody; // get page body by httpUrlConnection or okhttp or...
String html = FileUtils.readFromAssets("page.html", context);
// replace pageBody with ${body} in your html file
html = html.replace("${body}", pageBody);
WebView webView = (WebView) getView().findViewById(R.id.webView);
webView.getSettings().setAllowFileAccess(true);
WebSettings settings = webView.getSettings();
settings.setDefaultTextEncodingName("utf-8");
settings.setJavaScriptEnabled(true);
webView.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "utf-8", null);

how to display all size pages in one webview?

I'm devloping an apk to display some web pages by using webview.
The web pages have two size:
640*530
<meta name="page-view-size" content="640*530" />
it's diaplayed like this
1280*720
<meta name="page-view-size" content="1280*720" />
it's now displayed full screen
How can i display all size pages in fullscreen with one webview?
The device is version is Android 4.4.2
I've tried these methods:
WebSettings webSettings = mWebView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setPluginState(PluginState.ON);
webSettings.setSupportZoom(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
neither one is work for me.
Thanks.
you can try this for your web page header:
<meta id="viewport" name="viewport" width="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">

How To Open Webpage without addressbar in android

I use this code for open browser and open webpage with addressbar
String url = "http://www.google.com";
Itent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
But I want hide addressbar in my webpage
For this you can just use your WebView. Opening the default browser will always have an address bar.
The only way is to use a WebView with a WebViewClient. You open the system browser you can not hide the address bar
Try this:
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
For this you have to use WebView Client. This will surely work.
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl(url);
You can use WebView:
WebView wt;
wt.loadUrl("http://www.google.com/");
// Make sure that to prevent opening in browser with address bar,
// dont forget to add below line
wt.getSettings().setJavaScriptEnabled(true);
A basic example uses <meta name="mobile-web-app-capable" content="yes">
See: https://developer.chrome.com/multidevice/android/installtohomescreen
<!doctype html>
<html>
<head>
<title>Awesome app </title>
<meta name="viewport" content="width=device-width">
<meta name="mobile-web-app-capable" content="yes">
<link rel="icon" sizes="192x192" href="/icon.png">
</head>
<body></body>
</html>
as for opening it you can click on a link placed on the homescreen. This link is done by clicking the "add to home" option. I hope to find a way to automate the creation of this link...

Displaying Android asset files in a WebView?

I have seen various discussions on the problem of serving WebView pages from assets, none of which seemed definitive.
I want to be able to use a webview to display html (and included css) files stored in the project assets.
I have found that wv.loadUrl("file:///android_asset/html_no_copy/demo_welcome.html") displays that file okay, but links in demo_welcome.html, either local (no url prefixing the file name) or absolute - the same form as fed to loadUrl - don't work. They get a "Web page not available" error displayed on the view.
WebView wv = (WebView)this.findViewById(R.id.splashWebView);
wv.loadUrl("file:///android_asset/html_no_copy/test.html"); // Works
or
wv.loadUrl("file:///android_asset/html_no_copy/demo_welcome.html"); // Works
But neither of the following links in demo_welcome.html work:
CLICK HERE<p>
OR HERE
I know I can get around this by writing a content provider, but that seems extreme.
I want this to work from SDK 1.6 (4) on up.
Does anyone know if this can be done with just HTML, or does one need to kluge up some code to load the data?
Well, I found something that seems to work (on 1.6 and 2.2), in spite of a warning that it would recurse.
I also discovered that a css style-sheet link inside the first and second page both work without the following intercept. Odd and it makes me a bit nervous. Thoughts?
Here's the code:
WebView wv = (WebView)this.findViewById(R.id.splashWebView);
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
wv.loadUrl("file:///android_asset/html_no_copy/demo_welcome.html");
Here's the file contents:
demo_welcome.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Demo Html</title>
<link rel="stylesheet" type="text/css" href="demo.css" />
</head>
<body>
<H1>Testing One Two Three</H1>
CLICK HERE<p>
OR HERE
</body>
</html>
test.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" href="test.css" />
<title>Insert title here</title>
</head>
<body>
<H1>TEST.HTML</H1>
</body>
</html>
instead of loadUrl, try using the loadDataWithBaseURL method:
wv.loadDataWithBaseURL("fake://not/needed", html, mimeType, encoding, "");

Categories

Resources