이 튜토리얼에서는 Android 앱에서 ScrollView를 프로그래밍 방식으로 비활성화하는 방법을 단계별로 살펴봅니다. 핵심 아이디어는 간단합니다. ScrollView에 setOnTouchListener()를 등록해 터치 이벤트를 가로채면, 사용자가 화면을 밀어도 스크롤이 발생하지 않습니다.
1단계: 새 프로젝트 생성
Android Studio에서 File → New Project를 차례로 선택한 뒤, 안내에 따라 필요한 정보를 모두 입력하여 새 프로젝트를 만듭니다.
2단계: 레이아웃 작성 — res/layout/activity_main.xml
아래 코드를 activity_main.xml에 추가합니다. 여러 개의 버튼으로 화면보다 긴 콘텐츠를 구성하고, 이를 ScrollView로 감싸는 기본 구조입니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp">
<!-- 스크롤이 발생하도록 Button을 원하는 만큼 반복해서 추가하세요 -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
3단계: MainActivity.java 작성
아래 코드를 src/MainActivity.java에 추가합니다. 여기서 중요한 부분은 setOnTouchListener()입니다. 리스너의 onTouch()에서 true를 반환하면 터치 이벤트가 ScrollView에 전달되지 않아 스크롤이 비활성화됩니다.
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScrollView scrollView = findViewById(R.id.scrollView);
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(MainActivity.this, "ScrollView Disabled", Toast.LENGTH_SHORT).show();
return true; // 터치 이벤트를 소비하여 스크롤 차단
}
});
}
}
4단계: AndroidManifest.xml 설정
아래 코드를 androidManifest.xml에 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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 기기를 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 열고 툴바의 Run 아이콘을 클릭하세요. 실행 옵션에서 모바일 기기를 선택하면, 기기 화면에 아래와 같은 기본 화면이 나타납니다.

동작 원리
onTouch()에서 true를 반환하면 해당 터치 이벤트를 "소비(consume)"한 것으로 처리되어 ScrollView까지 전달되지 않습니다. 그 결과 스크롤 제스처는 무시되고, 대신 "ScrollView Disabled" 토스트 메시지가 표시됩니다.
참고: 더 유연한 대안
여러 화면에서 스크롤 잠금 기능을 자주 사용한다면, ScrollView를 상속하는 커스텀 클래스(예: LockableScrollView)를 만들고 setScrollingEnabled(boolean) 같은 메서드로 스크롤 여부를 자유롭게 제어하는 방식도 고려해 보세요. 조건에 따라 스크롤을 동적으로 켜고 꺼야 하는 경우 특히 유용합니다.