Tutorials  /  JavaScript

Android AutoCompleteTextView: Examples and Best Practices

Ccentron Redaktion · June 2024 ·3 min read ·JavaScript, Tutorial

In this blog post, you will learn how to implement AutoCompleteTextView in your Android app to simplify user input. We will guide you step-by-step on how to incorporate a list of suggestions using an ArrayAdapter and optimally utilize the key methods. Discover the benefits of this handy component and improve the user-friendliness of your app.

Using AutoCompleteTextView

An AutoCompleteTextView is used to display suggestions while typing in an editable text field. The suggestion list is shown in a dropdown menu from which the user can select the desired entry. The list of suggestions is provided by an adapter and appears only after a certain number of characters, which are set in the threshold (threshold). To use an AutoCompleteTextView, it must be defined in the XML layout as follows:

Code
<AutoCompleteTextView
    android:id="@+id/autoCompleteTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="65dp"
    android:ems="10" />
VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Key Methods

Some important methods of AutoCompleteTextView are:

  • getAdapter(): Returns the filterable list adapter used for auto-completion.
  • getCompletionHint(): Returns optional hint text displayed at the bottom of the matching list.
  • getDropDownAnchor(): Returns the ID of the view to which the auto-complete dropdown list is anchored.
  • getListSelection(): Returns the position of the dropdown view selection, if any.
  • isPopupShowing(): Indicates whether the popup menu is displayed.
  • setText(CharSequence text, boolean filter): Sets the text and can disable filtering.
  • showDropDown(): Displays the dropdown menu on the screen.

The setAdapter method is used to set the adapter of the AutoCompleteTextView. Let's look at the code snippet.

Project Structure for AutoCompleteTextView

This project contains a simple TextView and an AutoCompleteTextView in the layout of the MainActivity. The ArrayAdapter includes the following fruits: Apple, Banana, Cherry, Date, Grape, Kiwi, Mango, Pear.

Example Code for Android AutoCompleteTextView

The layout of the MainActivity is defined as follows (activity_main.xml):

Code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop />
</RelativeLayout>
Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren