안드로이드 WebView 확대/축소 컨트롤 활성화 방법
이 글에서는 안드로이드 앱에서 WebView의 확대/축소(Zoom) 컨트롤을 활성화하는 방법을 단계별로 소개합니다. WebView는 기본 상태에서는 핀치 줌이나 줌 버튼이 동작하지 않기 때문에, 몇 가지 설정을 직접 추가해 주어야 합니다.
1단계 — Android Studio에서 새 프로젝트 만들기
Android Studio를 열고 File → New Project 메뉴로 이동한 뒤, 필요한 항목을 모두 입력하여 새 프로젝트를 생성합니다.
2단계 — 레이아웃 파일 작성 (res/layout/activity_main.xml)
아래 코드를 activity_main.xml 파일에 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>위 레이아웃에는 tutorialspoint.com 페이지를 표시하기 위한 WebView 하나만 배치되어 있습니다.
3단계 — MainActivity.java 코드 작성
다음 코드를 src/MainActivity.java에 추가합니다.
package com.example.myapplication;
import android.app.ProgressDialog;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading Data...");
progressDialog.setCancelable(false);
WebView web_view = findViewById(R.id.web_view);
web_view.requestFocus();
web_view.getSettings().setLightTouchEnabled(true);
web_view.getSettings().setBuiltInZoomControls(true);
web_view.loadUrl("https://www.tutorialspoint.com/");
web_view.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if (progress < 100) {
progressDialog.show();
}
if (progress == 100) {
progressDialog.dismiss();
}
}
});
}
}여기서 핵심이 되는 부분은 다음과 같습니다.
- setBuiltInZoomControls(true) — WebView의 내장 확대/축소 기능을 켜는 가장 중요한 설정입니다.
- setDisplayZoomControls(true) — 화면에 +/− 버튼 형태의 줌 컨트롤을 함께 표시할 수 있습니다(기본값은 true).
- onProgressChanged() — 로딩 진행률이 100% 미만일 때는 ProgressDialog를 표시하고, 100%가 되면 닫습니다.
참고: 최신 Android Studio 프로젝트는 AndroidX를 사용하므로, import 문을androidx.appcompat.app.AppCompatActivity와androidx.annotation.RequiresApi로 변경해야 합니다.
4단계 — AndroidManifest.xml에 인터넷 권한 추가
외부 웹페이지를 불러오려면 인터넷 권한 선언이 반드시 필요합니다. 아래 코드를 AndroidManifest.xml에 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기를 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바의 Run 아이콘을 클릭하고, 목록에서 자신의 모바일 기기를 선택하세요. 그러면 기기 화면에 아래와 같은 결과가 표시됩니다.

앱이 실행되면 tutorialspoint.com 페이지가 로드되며, 화면의 줌 버튼 또는 핀치 제스처를 통해 페이지를 자유롭게 확대·축소할 수 있습니다.