উদাহরণের আগে জানতে হবে AutoCompleteTextView কী। এটি EditText-এর সাবক্লাস এবং সাধারণ EditText-এর মতই, কিন্তু ড্রপডাউন লিস্ট হিসেবে সাজেশন দেখায়। Threshold ভ্যালু সেট করতে হয়। উদাহরণস্বরূপ, Threshold ১ সেট করলে ব্যবহারকারী এক অক্ষর লিখলেই সাজেশন দেখাবে।
এই উদাহরণটি AutoCompleteTextView-এ অ্যাডাপ্টার সেট করার পদ্ধতি দেখায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
অ্যান্ড্রয়েড স্টুডিওতে File → New Project এ যান এবং প্রজেক্ট তৈরির জন্য প্রয়োজনীয় বিবরণ পূরণ করুন।
ধাপ ২: 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:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/autoComplete"
android:layout_width="fill_parent"
android:hint="Enter programming language"
android:layout_height="wrap_content" />
</LinearLayout>
ব্যবহারকারী অক্ষর লিখলে ড্রপডাউন মেনুতে সাজেশন দেখাবে।
ধাপ ৩: MainActivity.java-এ কোড যোগ করুন
package com.example.andy.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RadioButton radioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoComplete);
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Android");
arrayList.add("JAVA");
arrayList.add("CPP");
arrayList.add("C Programming");
arrayList.add("Kotlin");
arrayList.add("CSS");
arrayList.add("HTML");
arrayList.add("PHP");
arrayList.add("Swift");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, arrayList);
autoCompleteTextView.setAdapter(arrayAdapter);
autoCompleteTextView.setThreshold(1);
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d("beforeTextChanged", String.valueOf(s));
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("onTextChanged", String.valueOf(s));
}
@Override
public void afterTextChanged(Editable s) {
Log.d("afterTextChanged", String.valueOf(s));
}
});
}
}
ArrayList-এ মান সংরক্ষণ করে ArrayAdapter-এ অ্যাপেন্ড করা হয়েছে। AutoCompleteTextView-এ অ্যাডাপ্টার সেট করে Threshold ১ দেওয়া হয়েছে।
আউটপুট
প্রাথমিক স্ক্রিন:

"ja" লিখলে সাজেশন:

"J" মুছে "c" লিখলে একাধিক সাজেশন:
