Reputation: 1548
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);
}
});
}
}
Upvotes: 8
Views: 10405
Reputation: 1223
mWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setBuiltInZoomControls(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(true);
mWebView.setInitialScale(400); // zoom x4 (400%)
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
Upvotes: 0
Reputation: 35
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
Upvotes: 2
Reputation: 26961
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.
Upvotes: 4
Reputation: 5260
webView.getSettings().setBuiltInZoomControls(true);
If you need more advance webView
you can use webview sample.
Upvotes: 3