Enable Android WebView Scaling

I cannot enable zoom clamp in WebView in Android

I am new to programming, have already tried everything and can not solve the problem.

Being new here, I cannot attach an 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); } }); } } 
+6
source share
3 answers

To enable scaling in the webView, add the following code to the onTuchEvent override method:

 webview.getSettings().setBuiltInZoomControls(true); webview.getSettings().setSupportZoom(true); 

If your webview goes to a different URL and you want to keep the zoom level use the webSettings class

 webview.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR); 

If you want the preset zoom to be used when loading webview :

 mWebView.setInitialScale(ZOOM_LEVEL); // int value 50 = 50% -- 200 = 200% 

This works well for all device resolutions.

UPDATE: If any of these work (weeeird ...), try the following:

 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.

+2
source
 webView.getSettings().setBuiltInZoomControls(true); 

If you need more advance webView , you can use a sample web view .

+3
source
 webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setDisplayZoomControls(false); 
+1
source

Source: https://habr.com/ru/post/984615/


All Articles