ऑटोकंप्लीट टेक्स्ट इनपुट लागू करना
यह ट्यूटोरियल AutoCompleteTextView (AppCompat वर्जन) का उपयोग करके ड्रॉपडाउन सुझावों वाला टेक्स्ट फील्ड बनाने का तरीका दिखाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट XML (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="फल का नाम दर्ज करें"
android:textColor="#000000"
android:textColorHint="#000000" />
</LinearLayout>
चरण 3: 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 adapter = new ArrayAdapter<>(
this,
android.R.layout.select_dialog_item,
fruits
);
autoTextView.setThreshold(1); // पहले कैरेक्टर से सुझाव शुरू
autoTextView.setAdapter(adapter);
}
}
चरण 4: 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): सुझाव दिखाने से पहले न्यूनतम कैरेक्टर्स (डिफ़ॉल्ट 2)setAdapter(Adapter): डेटा स्रोत सेट करता हैsetDropDownHeight()/Width(): ड्रॉपडाउन आकार कस्टमाइज करेंsetOnItemClickListener(): चयन पर एक्शन लें
एडवांस: कस्टम एडाप्टर
कस्टम लेआउट/फिल्टरिंग के लिए ArrayAdapter या CursorAdapter को एक्सटेंड करें और getView() / runQueryOnBackgroundThread() ओवरराइड करें।
आउटपुट
टाइप करते ही मिलते-जुलते फल के नाम ड्रॉपडाउन में दिखाई देंगे।
