Loading images in WebView in android studio
We can use WebView for loading images from any url or for loading images from drawable folder, assets folder, or resource folder of our app.
1. To load an image from a website in WebView, we can directly write the url of the image in the WebView loadUrl block. Even animated gifs can be loaded directly to WebView.
WebView webview1 loadUrl http://.....jpg
2. We can modify the width and height of this image loaded in WebView by using following code in WebView loadUrl block:
WebView webview1 loadUrl data:text/html,<img width=100% height="" src="(imageUrl)">
7. To enable zooming images or HTML files loaded in WebView, use an add source directly block in onCreate of the project and put following code in it:
The code above enables pinch zoom in WebView. It is applicable to anything loaded in WebView and not just the image. It also loads the WebView with overview mode which means the width of the page fits the screen width when loaded initially, but can be zoomed.
8. To remove the ScrollBar of WebView which appears while zooming WebView, use following code in onCreate event using add source directly block:
1. To load an image from a website in WebView, we can directly write the url of the image in the WebView loadUrl block. Even animated gifs can be loaded directly to WebView.
WebView webview1 loadUrl http://.....jpg
- webView.loadUrl("file:///android_assets/error.html");
- webView.loadUrl("file:///android_assets/error.html");
WebView webview1 loadUrl data:text/html,<img width=100% height="" src="(imageUrl)">
- webView.loadUrl("data:text/html,<img width=100% height="" src="file://locahost://neo/logo.png");
- webView.loadUrl("data:text/html,<img width=100% height="" src="file://locahost://neo/logo.png");
Note that in above code image is name of the image and has to be changed accordingly.
Note that animations do not work with this method.
4. Another way to load an image in WebView is to add a sound using sound manager, and then replace it with an image with same name and extension (.mp3) in the.
asstent folder.
After that the image can be loaded in WebView using following block:
WebView webview1 loadUrl file:///android_res/raw/image
- webView.loadUrl("file:///android_assets/image.png");
- webView.loadUrl("file:///android_assets/image.png");
- WebSettings webSettings = webview1.getSettings();
- webSettings.setJavaScriptEnabled(true); webSettings.setUseWideViewPort(true);
- webSettings.setLoadWithOverviewMode(true);
- webSettings.setSupportZoom(true);
- webSettings.setBuiltInZoomControls(true);
- webSettings.setDisplayZoomControls(false);
- WebSettings webSettings = webview1.getSettings();
- webSettings.setJavaScriptEnabled(true); webSettings.setUseWideViewPort(true);
- webSettings.setLoadWithOverviewMode(true);
- webSettings.setSupportZoom(true);
- webSettings.setBuiltInZoomControls(true);
- webSettings.setDisplayZoomControls(false);
8. To remove the ScrollBar of WebView which appears while zooming WebView, use following code in onCreate event using add source directly block:
- webview1.setVerticalScrollBarEnabled(false);
- webview1.setHorizontalScrollBarEnabled(false);
- webview1.setVerticalScrollBarEnabled(false);
- webview1.setHorizontalScrollBarEnabled(false);
0 Comments: