AutoComplete Text
AutoComplete Text
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center"
android:background="@android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search Engine"
android:textSize="24sp"
android:textStyle="bold"
android:layout_marginBottom="20dp"
android:textColor="@android:color/black"/>
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Search..."
android:textSize="18sp"
android:padding="10dp"
android:background="@android:drawable/edit_text"
android:completionThreshold="1"/>
</LinearLayout>
package com.example.searchengine;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] searchSuggestions = {
"Android Development",
"Artificial Intelligence",
"Machine Learning",
"Big Data",
"Cybersecurity",
"Web Development",
"Data Science",
"Software Engineering",
"Cloud Computing"
};
autoCompleteTextView.setAdapter(adapter);
Explanation:
The completionThreshold="1" ensures suggestions appear after typing just one character.
package com.example.searchengine;
package: Defines the namespace of your app. Here, com.example.searchengine is the package
name automatically created when you set up your project in Android Studio.
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import androidx.appcompat.app.AppCompatActivity;
import statements: Bring in external classes required for the app to function.
ArrayAdapter: Bridges data (like a list of search suggestions) with a view (like
AutoCompleteTextView).
AppCompatActivity: A base class for activities, providing backward compatibility with older
Android versions.
public class MainActivity: This is the main activity where the app starts running.
@Override
protected void onCreate(Bundle savedInstanceState): This method runs when the activity is
created. It’s like the entry point of this screen. The Bundle helps restore the activity’s previous
state if it was paused or stopped.
super.onCreate(savedInstanceState);
setContentView(): Tells Android which layout to display for this activity. Here, it refers to
activity_main.xml, where the AutoCompleteTextView is defined.
AutoCompleteTextView autoCompleteTextView =
findViewById(R.id.autoCompleteTextView);
String[] searchSuggestions = {
"Android Development",
"Artificial Intelligence",
"Machine Learning",
"Big Data",
"Cybersecurity",
"Web Development",
"Data Science",
"Software Engineering",
"Cloud Computing"
};
String[] searchSuggestions: An array of search suggestions. These will be shown in the dropdown
list as the user starts typing.
Parameters:
setAdapter(): Links the ArrayAdapter to the AutoCompleteTextView. This enables the auto-
suggestion feature.
As the user types, matching suggestions from searchSuggestions will appear in a dropdown list.
These lines close the onCreate() method and the MainActivity class.
Binds data (like strings, numbers, objects) to views (dropdown lists, text fields, etc.).
⚡ Basic Syntax:
autoCompleteTextView.setAdapter(adapter);
Now, when the user types something, auto-suggestions from the languages array will appear.
Or even override methods like getView() if you want to style each item differently.