Implementation of AutoCompleteTextView and EditText in Android

Experiment Title

Implementation of AutoCompleteTextView and EditText in Android


Aim

To develop an Android application that demonstrates the use of EditText for normal text input and AutoCompleteTextView for providing suggestions while typing.


Software Requirements

  • Android Studio (Latest Stable Version)

  • Java Development Kit (JDK)

  • Android SDK (API 21 or above)

  • Medium Phone AVD (Pixel / Medium Phone, API 30+)


Project Details

  • Project Name: AutoCompleteEditTextDemo

  • Activity Name: MainActivity

  • Layout File: activity_main.xml

  • Language: Java


Conceptual Overview

EditText

EditText is a user interface component used to accept text input from the user, such as name, email, or password.

AutoCompleteTextView

AutoCompleteTextView is an editable text view that displays a list of suggestions automatically while the user types. Suggestions are provided using an Adapter.

ArrayAdapter

ArrayAdapter acts as a bridge between the data source (array of strings) and the UI component (AutoCompleteTextView).


Step 1: XML Layout Design

File: activity_main.xml

<?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="20dp">

    <!-- Title TextView -->
    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AutoCompleteTextView and EditText Demo"
        android:textSize="20sp"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_marginBottom="20dp" />

    <!-- EditText for normal input -->
    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter your name"
        android:inputType="textPersonName"
        android:layout_marginBottom="15dp" />

    <!-- AutoCompleteTextView -->
    <AutoCompleteTextView
        android:id="@+id/actLanguage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Enter programming language"
        android:layout_marginBottom="20dp" />

    <!-- Button -->
    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>

Step 2: Java Code Implementation

File: MainActivity.java

package com.example.autocompleteedittextdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText etName;
    AutoCompleteTextView actLanguage;
    Button btnSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize UI components
        etName = findViewById(R.id.etName);
        actLanguage = findViewById(R.id.actLanguage);
        btnSubmit = findViewById(R.id.btnSubmit);

        // Data source for AutoCompleteTextView
        String[] languages = {
                "Android",
                "Java",
                "Kotlin",
                "Python",
                "C",
                "C++",
                "Flutter"
        };

        // Create ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
                android.R.layout.simple_dropdown_item_1line,
                languages
        );

        // Set adapter and threshold
        actLanguage.setAdapter(adapter);
        actLanguage.setThreshold(1);

        // Button click event
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String name = etName.getText().toString();
                String language = actLanguage.getText().toString();

                // Display entered values
                Toast.makeText(
                        MainActivity.this,
                        "Name: " + name + "\nLanguage: " + language,
                        Toast.LENGTH_LONG
                ).show();
            }
        });
    }
}

Step 3: Execution and Testing

  1. Launch Android Studio

  2. Run the project on Medium Phone AVD

  3. Enter text in:

    • EditText → accepts normal input

    • AutoCompleteTextView → displays suggestions after typing one character

  4. Select a suggestion and click Submit

  5. Verify output using Toast message


Result

The Android application successfully demonstrates:

  • Use of EditText for user input

  • Use of AutoCompleteTextView for suggestion-based input

  • Proper integration of ArrayAdapter

  • Correct execution on a Medium Phone AVD

Thus, the experiment satisfies LLO 4.1.


Viva / Exam-Oriented Points

  • setThreshold(1) defines minimum characters to start suggestions

  • ArrayAdapter connects data to AutoCompleteTextView

  • AutoCompleteTextView improves user experience and reduces typing errors


Conclusion

The application is error-free, beginner-friendly, and academically suitable for Android practical examinations.


DirectoryStructureDemo

├── manifests

│   └── AndroidManifest.xml

├── java

│   └── com.example.directorystructuredemo

│       └── MainActivity.java

├── res

│   ├── layout

│   │   └── activity_main.xml

│   │

│   ├── values

│   │   ├── strings.xml

│   │   ├── colors.xml

│   │   └── themes.xml

│   │

│   ├── drawable

│   │   └── (images, shapes, vectors)

│   │

│   └── mipmap

│       └── (app launcher icons)

└── Gradle Scripts

    ├── build.gradle (Project)

    ├── build.gradle (Module: app)

    └── settings.gradle


Popular posts from this blog