এই টিউটোরিয়ালে আমরা শিখব কীভাবে AutoCompleteTextView ব্যবহার করে ব্যবহারকারীর টাইপ করার সাথে সাথে সাজেশন দেখানো যায়।
প্রয়োজনীয় ধাপগুলো
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ফাইল তৈরি করুন
res/layout/activity_main.xml ফাইলে নিচের কোড যোগ করুন:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_margin="16dp"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="@+id/autoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter fruit name"
android:textColor="#000000"
android:textColorHint="#000000" />
</LinearLayout>
ধাপ ৩: মেইন এক্টিভিটিতে অडेप্টার সেট করুন
src/MainActivity.java ফাইলে নিচের কোড যোগ করুন:
package app.tutorialspoint.com.sample;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatAutoCompleteTextView;
import android.widget.ArrayAdapter;
public class MainActivity extends AppCompatActivity {
private String[] fruits = {"Apple", "Banana", "Cherry", "Date", "Grape", "Kiwi", "Mango", "Pear"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AppCompatAutoCompleteTextView autoTextView = findViewById(R.id.autoTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.select_dialog_item, fruits);
autoTextView.setThreshold(1); // প্রথম ক্যারেক্টার থেকেই কাজ করবে
autoTextView.setAdapter(adapter);
}
}
ধাপ ৪: ম্যানিফেস্ট কনফিগার করুন
AndroidManifest.xml ফাইল:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.com.sample">
<uses-permission android:name="android.permission.VIBRATE" />
<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>
গুরুত্বপূর্ণ অ্যাট্রিবিউট ও মেথড
setThreshold(int)- কতটি ক্যারেক্টার টাইপের পর সাজেশন শুরু হবে (ডিফল্ট ২)setAdapter(Adapter)- ডেটা সোর্স বাইন্ড করতেsetDropDownBackgroundResource(int)- ড্রপডাউন ব্যাকগ্রাউন্ড রঙsetDropDownHeight(int)- ড্রপডাউন উচ্চতাsetOnItemClickListener()- আইটেম সিলেক্ট হলে ইভেন্ট হ্যান্ডলিং
কাস্টম অडेপ্টার ব্যবহার (উন্নত)
কাস্টম লেআউটে ইমেজ বা একাধিক টেক্সট দেখাতে ArrayAdapter এর সাবক্লাস তৈরি করুন:
public class FruitAdapter extends ArrayAdapter<Fruit> {
// getView() ওভাররাইড করে কাস্টম লেআউট ইনফ্লেট করুন
}
Material Components versão (সুপারিশ করা)
Material Design 3-এ AutoCompleteTextView এর পরিবর্তে com.google.android.material.textfield.MaterialAutoCompleteTextView ব্যবহার করুন, যা আউট-অফ-বক্স স্টাইলিং এবং অ্যাক্সেসিবিলিটি প্রদান করে।