android webview zoom /pinch to zoom android studio
Hello android developer, in this article we will learn how to enable pinch to zoom on webview. using this source code you can enable WebView zoom.
please follow the code given below to enable pinch to zoom on webview.
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; public class zoominWebView extends AppCompatActivity { private WebView m; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zoomin_web_view); m=(WebView) findViewById(R.id.webView); WebSettings webSettings =m.getSettings(); m.getSettings().setBuiltInZoomControls(true); m.setWebViewClient(new WebViewClient()); m.loadUrl("https://www.google.com/"); } } |
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".after20.zoominWebView"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> </WebView> </android.support.constraint.ConstraintLayout> |
Output:
Finally, run this project.
This is how the application looks like.