How to set android webview zoom to specific height - android

I am using a Webview to look at a local image with zoom support and all that. Is there any way that I can make the WebView zoom to only fit the height of the image, instead of the width like it does with my current code? Here is what I have:
WebView image = (WebView)findViewById(R.id.map);
image.getSettings().setBuiltInZoomControls(true);
image.getSettings().setLoadWithOverviewMode(true);
image.getSettings().setUseWideViewPort(true);
image.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
image.loadUrl("file:///android_asset/image.png");

Try using the webView Settings
use the webSettings class
webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
Check out Set zoom for Webview
Update:
as setDefaultZoom deprecated you need to use another settings

Related

Load url with minimum zooming width - android

I have a web view and when it is loading URL, it is already zoomed in with some percentage. i.e. user has to scroll horizontally to see the entire content. I want to load the web page with minimum zooming size. i.e. user cannot be zoom out anymore at the time of loading. I tried below, but it is showing the entire page in the web view at once. ultimately I want to fit the web page horizontally.
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
Just use setMinimumFontSize and setUseWideViewPort(true) like below. (Y)
WebView wv = (WebView) findViewById(R.id.webview1);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.getSettings().setMinimumFontSize(35);

How to show Certain Part of WebPage inside Webview With Fit Screen to All Devices

I Want to Show WebPage Inside WebView. Up to this it is fine.
But I have WebPage (As Given Below) and I want Certain Part of it. I mean just Top Left Corner should be visible in fit to screen mode in all the devices.
I'm resolving this Problem since last two three days.
What I want : I want to Show Top Left Corner of webpage to show inside Webview by Whatever the Stratergy May be with zoom or any other Option but it Should be fit to screen for all the device.
One More thing I can't change the Source Code of Webpage, because it is fixed.
I have to manage by code itself.
What I have Tried : I have refer Stackoverflow links but no luck.
Here is my Code. :
WebView wv;
wv = (WebView) findViewById(R.id.webview_MyPoops);
wv.getSettings().setJavaScriptEnabled(true);
// wv.setInitialScale(185);
// wv.setInitialScale(30);
String URL = "MY_LINK";
wv.loadUrl(URL);
WebSettings webSettings = wv.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
I'm getting complete webpage shown in devices.
It Works fine in My LG Device(Which has Resolution of 480*800) but in big device or tabs like Galaxy Tab2, I'm getting very small image of Top Left Corner and all other Area of screen are just background of WebPage.
Below is the screenshot of Galaxy Tab2 after Running the above code.
Please have you Any suggestion where I am lacking. Thanks in Advance.
try with this code hop this work for u.
webview.setInitialScale(getScale());
private int getScale(){
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
Double val = new Double(width)/new Double(200);
Log.e("hh", "** "+val);
val = val * 100d;
Log.e("hh", "** "+val);
return val.intValue();
}
You could try using HtmlCleaner (examples of how to use it here) to get the html from the webpage, then extract the part you want to display and load it into the WebView using: WebView.loadData();.
I used this method in one of my apps to reformat a page so that it would be easier to use on a mobile device.
On the HTML page you can have something like target-densitydpi=240;
depending on device type just set this parameter from your Java code, the page will automatically fit to target density.
I believe the fix should be done only at the web page level and ideally WebView cannot do any tricks here. WebView is simply a browser(to be specific chromium browser) which should just do rendering as instructed by the web page.
In order to actually fix your issue, you should try modifying the CSS of the web page.
WebView x;
x.setInitialScale(1);
This is the furtheset zoom possible. But for some sites it just looks pure UGLY.
This was the second version I found
test1.getSettings().setDefaultZoom(ZoomDensity.FAR);
Thats a nice all rounder than seems to just zoom out far enough for a lot but still not what I was looking for.
And now here is the final solution I have.
x.getSettings().setLoadWithOverviewMode(true);
x.getSettings().setUseWideViewPort(true);
Basically what these do is answered in another question like this.
setLoadWithOverviewMode(true)
Loads the WebView completely zoomed out
setUseWideViewPort(true)

Android Webview: Images that are too large for the screen

I'm looking for a way to have images appear in a WebView that initially fits the screen, instead of showing the actual size. Is there a way to do this?
How is it different if:
1) I open an html file with an img tag to the image
2) I open the image file directly
Thanks for any assistance.
I ended up using:
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
To make the contents of the page fit to the WebView.
If I understand the question, you have an image on a web page that has dimensions X x Y and when you load the webpage into a WebView you want it to take up the dimensions of the android device (X1 x Y1). To do this pass a querystring to the url which renders the height and the width of the image at 100%. Also have the layout_width and layout_height of the webview set to the appropriate values.
If you load the image directly into the WebView, you can set the height and width of the image
You can use Webview's LayoutAlgorithm. But this will resize only those images with Width greater than the width of the device. This won't help you with images smaller than the screen.
Link: Can Android's WebView automatically resize huge images?
Another Solution is using a Javascript or an External Library like HTML Cleaner to remove the
<img> size attributes and then change their size via code and getWidth().

WebView to be exact to the screen size in android app

in my app i am trying to load a webpage. The webpage contains an image. I want the image to be exactly within the android device screen size. I want the image to be fitted to the evice.
In the web contents we have added the view port tag. I have loaded the URL in default browser of the device,the web page looked to be fitted in 2.1 but in 2.2 version the image seems to be very large. So i loaded the URL in a web view as follows
webView = (WebView)findViewById(R.id.webView1);
WebSettings webSettings = webView.getSettings();
webSettings.setUseWideViewPort(true);
webView.setInitialScale(30);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://xxxxxxxxxxx/get_ban/3");
Now the image is looking good in 2.2 version and in 2.1 it looks very small.
I want the image to be fitted to the screen size common for all the os version, how to do this......
try to use this
setLoadWithOverviewMode(true);
Is it the effect?
Try to use it with
setUseWideViewPort(true);

using pinch in the webview android

I have two questions:
I find this code to use pinch in the webImageView but when I change webImageView by WebView. I obtain an error in the code. how I use pinch in the webview??
// create the WebImageView object from xml
WebImageView img = (WebImageView) findViewById(R.id.main_pic);
// fetches the image in a background thread
img.setImageFromURL("http://www.mysite.com/mypicture.jpg");
// enable pinch-zoom abilities on the image
new PinchImageView(img);
When I add pinch in my code is that supported by all the mobile android or you should have a specific screen to make it work it.
You can enable zooming in a WebView by following WebView setting
mWebView.getSettings().setBuiltInZoomControls(true);
Also you can set the default zoom level by using ZoomDensity
mWebView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.CLOSE);
Have you added the referencing library of Nikko?
http://code.google.com/p/android-pinch/wiki/PinchImageView

Categories

Resources