ওভারভিউ
এই উদাহরণটি দেখায় কিভাবে একটি ScrollView-কে প্রোগ্রাম্যাটিক্যালি নিষ্ক্রিয় (disable) করা যায়। এতে OnTouchListener ব্যবহার করে টাচ ইভেন্ট ব্লক করা হয়েছে, ফলে স্ক্রলিং বন্ধ থাকে।
পূর্বশর্ত
- অ্যান্ড্রয়েড স্টুডিও সেটআপ
- বেসিক জাভা ও XML লেআউট জ্ঞান
ধাপ ১: প্রজেক্ট তৈরি করুন
অ্যান্ড্রয়েড স্টুডিওতে File → New Project → Empty Activity সিলেক্ট করে প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ফাইল (res/layout/activity_main.xml)
<?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="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 1" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 2" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 3" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 4" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 5" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 6" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 7" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 8" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 9" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 10" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 11" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 12" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 13" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 14" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 15" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 16" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 17" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 18" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 19" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button 20" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
ধাপ ৩: MainActivity.java
package com.example.scrollviewdisable;
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 {
@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; // true রিটার্ন করলে টাচ ইভেন্ট কনসাম হয়, স্ক্রলিং বন্ধ থাকে
}
});
}
}
ব্যাখ্যা: onTouch()-এ true রিটার্ন করলে টাচ ইভেন্ট ScrollView-এ পৌঁছায় না, ফলে স্ক্রলিং ডিজেবল হয়।
ধাপ ৪: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.scrollviewdisable">
<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>
চলান ও পরীক্ষা
অ্যাপ চালানোর পর ScrollView-এ স্বাইপ করলে "ScrollView Disabled" টোস্ট দেখাবে এবং কন্টেন্ট স্ক্রল হবে না।
বিকল্প পদ্ধতি
আপনি scrollView.setEnabled(false) দিয়েও স্ক্রলিং বন্ধ করতে পারেন, তবে সেটি ভিজ্যুয়ালি ডিজেবল দেখাবে (গ্রে আউট)। টাচ লিসেনার পদ্ধতিতে UI স্বাভাবিক থাকে কিন্তু ইন্টারঅ্যাকশন বন্ধ থাকে।