Android AutoCompleteTextView: Examples and Best Practices

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:

<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" />

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

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

Source: digitalocean.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in:

Moderne Hosting Services mit Cloud Server, Managed Server und skalierbarem Cloud Hosting für professionelle IT-Infrastrukturen

Secure HTTPS Setup on Arch Linux with Apache or Nginx

Security, Tutorial

Linux file permissions with this comprehensive guide. Understand how to utilize chmod and chown commands to assign appropriate access rights, and gain insights into special permission bits like SUID, SGID, and the sticky bit to enhance your system’s security framework.