0% found this document useful (0 votes)
5 views6 pages

AutoComplete Text

The document describes an Android application that features an AutoCompleteTextView for search suggestions. It includes a layout defined in XML and a MainActivity class that sets up the AutoCompleteTextView with predefined search suggestions using an ArrayAdapter. The application allows users to receive suggestions as they type, enhancing the search experience.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

AutoComplete Text

The document describes an Android application that features an AutoCompleteTextView for search suggestions. It includes a layout defined in XML and a MainActivity class that sets up the AutoCompleteTextView with predefined search suggestions using an ArrayAdapter. The application allows users to receive suggestions as they type, enhancing the search experience.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

<?xml version="1.0" encoding="utf-8"?

>

<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

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);

// Sample search suggestions

String[] searchSuggestions = {

"Android Development",

"Artificial Intelligence",

"Machine Learning",

"Big Data",

"Cybersecurity",

"Web Development",

"Data Science",

"Software Engineering",

"Cloud Computing"

};

// Setting up the adapter for AutoCompleteTextView

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_dropdown_item_1line, searchSuggestions);

autoCompleteTextView.setAdapter(adapter);

Explanation:

 AutoCompleteTextView allows users to get search suggestions as they type.

 The ArrayAdapter is used to provide predefined search suggestions.

 The completionThreshold="1" ensures suggestions appear after typing just one character.

 The UI is minimal, with a title and an AutoCompleteTextView field.


📄 MainActivity.java

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.

Bundle: Used to pass data between Android activities.

ArrayAdapter: Bridges data (like a list of search suggestions) with a view (like
AutoCompleteTextView).

AutoCompleteTextView: The UI component that provides auto-suggestions as the user types.

AppCompatActivity: A base class for activities, providing backward compatibility with older
Android versions.

public class MainActivity extends AppCompatActivity {

public class MainActivity: This is the main activity where the app starts running.

extends AppCompatActivity: Inherits features from AppCompatActivity, allowing us to use


modern Android features with backward compatibility.

@Override

protected void onCreate(Bundle savedInstanceState) {

@Override: Indicates we're overriding the onCreate method from AppCompatActivity.

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);

super.onCreate(savedInstanceState): Calls the parent class’s onCreate() method. This is


necessary to ensure the activity is set up correctly before adding custom code.
setContentView(R.layout.activity_main);

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);

findViewById(): Finds the AutoCompleteTextView from the layout using its ID


(@+id/autoCompleteTextView).

We store a reference to it in the variable autoCompleteTextView so we can manipulate it in the


code (like setting suggestions).

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.

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_dropdown_item_1line, searchSuggestions);

ArrayAdapter<String>: Acts as a bridge between the data (searchSuggestions) and the


AutoCompleteTextView.

Parameters:

this: Refers to the current MainActivity context.

android.R.layout.simple_dropdown_item_1line: A predefined Android layout for dropdown


items (simple and clean).

searchSuggestions: The array of suggestions to display.


autoCompleteTextView.setAdapter(adapter);

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.

🚀 How It Works Together:

When the app starts, the MainActivity is created.

The activity_main.xml layout is displayed.

The AutoCompleteTextView is linked to the code.

An array of search suggestions is prepared.

The ArrayAdapter connects these suggestions to the AutoCompleteTextView.

When you start typing, suggestions appear automatically!

📦 What is an ArrayAdapter in Java?

An ArrayAdapter in Java (especially in Android development) is a bridge between a data source


(like an array or a list) and a UI component (like ListView, Spinner, or AutoCompleteTextView).
It helps display data from an array or list into the UI in a simple, structured way.

🔑 Key Features of ArrayAdapter:

 Binds data (like strings, numbers, objects) to views (dropdown lists, text fields, etc.).

 Works with simple UI elements like ListView, Spinner, and AutoCompleteTextView.

 Supports custom layouts if you need more complex designs.

⚡ Basic Syntax:

ArrayAdapter<Type> adapter = new ArrayAdapter<>(Context, LayoutResource, DataSource);

 Type: The type of data (like String, Integer, CustomObject, etc.).

 Context: Refers to the current activity or fragment (this or getContext()).

 LayoutResource: Defines how each item will look (e.g., android.R.layout.simple_list_item_1).


 DataSource: The actual data (an array or list).

🎯 Example: Using ArrayAdapter with AutoCompleteTextView

1️⃣ Data Source (Array of Strings):

String[] languages = {"Java", "Python", "C++", "Kotlin", "JavaScript"};

2️⃣ ArrayAdapter Setup:

ArrayAdapter<String> adapter = new ArrayAdapter<>(this,


android.R.layout.simple_dropdown_item_1line, languages);

 this = Current context (e.g., MainActivity).

 android.R.layout.simple_dropdown_item_1line = Built-in layout with a simple text view.

 languages = Data to display.

3️⃣ Connecting Adapter to AutoCompleteTextView:

AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);

autoCompleteTextView.setAdapter(adapter);

Now, when the user types something, auto-suggestions from the languages array will appear.

💡 How Does It Work Behind the Scenes?

1. User starts typing in the AutoCompleteTextView.

2. The ArrayAdapter checks the data source for matches.

3. Matching suggestions appear as a dropdown list.

4. When a user selects an item, the text is auto-filled.

🔍 Customizing ArrayAdapter (Advanced Use):

You can create custom designs by using a custom layout:

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.custom_layout, languages);

Or even override methods like getView() if you want to style each item differently.

You might also like