I can not enable the zoom clamp on WebView in Android
I am new to programming, already tried everything and can not solve the problem.
Being new here , I can not attach image.
WebViewActivity.java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setBuiltInZoomControls(true);
webSettings.setSupportZoom(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://200.99.150.163/sacsptrans/hrecinicial.aspx");
webView.setWebViewClient(new WebViewClient()
{
// prevent open in external browser
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
});
}
MainActivity.java
package com.mkyong.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
});
}
}
webSettings. setBuiltInZoomControls(true); webSettings. setSupportZoom(true);
getSettings(). setBuiltInZoomControls(true); per this answer to default my view to zoomed out and to be able to pinch-to-zoom.
In Android, Zoom Control is a class that has some set of methods that are used to control the zoom functionality. It has two buttons that are used to control the zoom functionality (ie Zoom In and Zoom Out). Zoom Control Class has been deprecated in API Version 29.
In order to enable zoom on the webView, add the following code in onTuchEvent override method:
webview.getSettings().setBuiltInZoomControls(true);
webview.getSettings().setSupportZoom(true);
If your webview
goes to another url and you want to mantain the zoom level
use the webSettings class
webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
If you want a pre-defined zoom when webview
loads use:
mWebView.setInitialScale(ZOOM_LEVEL); // int value 50 = 50% -- 200 = 200%
It works pretty well for all device resolutions.
UPDATE: If any of those are working (weeeird...) try this:
public class Main extends Activity {
private WebView myWebView;
private static final FrameLayout.LayoutParams ZOOM_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,Gravity.BOTTOM);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.webview);
this.myWebView = (WebView) this.findViewById(R.id.webView);
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.myWebView.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.GONE);
this.myWebView.loadUrl("http://www.facebook.com");
}
}
From this question.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With