AutoCompleteTextView-AGN HUB

AutoCompleteTextView in Kotlin

AutoCompleteTextView is an editable text view that shows suggestions to the user as they type. The suggestions are displayed in a dropdown menu, and the user can select one to autocomplete the input field.

Key Properties

  1. threshold: Minimum number of characters the user must type before suggestions appear.

  2. adapter: Supplies the data (suggestions) for the dropdown menu.

  3. setOnItemClickListener: Detects when a suggestion is selected.


Basic Implementation

XML Layout

<AutoCompleteTextView

    android:id="@+id/autoCompleteTextView"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:hint="Enter a name"

    android:completionThreshold="1" />

Kotlin Code

import android.os.Bundle

import android.widget.ArrayAdapter

import android.widget.AutoCompleteTextView

import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)


        // Reference to AutoCompleteTextView

        val autoCompleteTextView: AutoCompleteTextView = findViewById(R.id.autoCompleteTextView)


        // Data source for suggestions

        val names = listOf("Alice", "Bob", "Charlie", "David", "Emily")


        // Adapter to bind data to the AutoCompleteTextView

        val adapter = ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, names)

        autoCompleteTextView.setAdapter(adapter)


        // Handle item selection from suggestions

        autoCompleteTextView.setOnItemClickListener { parent, _, position, _ ->

            val selectedName = parent.getItemAtPosition(position).toString()

            // Handle the selected name

            println("Selected: $selectedName")

        }

    }

}


Customizing Suggestions

  1. Custom Layout for Suggestions You can define a custom layout for the dropdown items.

  2. <!-- custom_dropdown_item.xml -->

  3. <TextView xmlns:android="http://schemas.android.com/apk/res/android"

  4.     android:layout_width="match_parent"

  5.     android:layout_height="wrap_content"

  6.     android:padding="8dp"

  7.     android:textColor="@color/black"

  8.     android:textSize="16sp" />

In Kotlin:

val adapter = ArrayAdapter(this, R.layout.custom_dropdown_item, names)

autoCompleteTextView.setAdapter(adapter)

  1. Filtering Suggestions You can implement a custom Filter to dynamically control the suggestions.


Advanced Example: Using with a Database or API

For dynamic suggestions (e.g., from a database or API), you need to fetch and set the adapter's data programmatically.

autoCompleteTextView.addTextChangedListener(object : TextWatcher {

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        val query = s.toString()

        // Fetch suggestions based on query

        fetchSuggestions(query) { suggestions ->

            val adapter = ArrayAdapter(this@MainActivity, android.R.layout.simple_dropdown_item_1line, suggestions)

            autoCompleteTextView.setAdapter(adapter)

        }

    }

    override fun afterTextChanged(s: Editable?) {}

})


Common Use Cases

  1. Search Fields: Autocomplete for searching items.

  2. Form Inputs: Cities, names, email providers, etc.

  3. Autocomplete with Icons: Combine AutoCompleteTextView with custom layouts to include icons or additional details.

Would you like an example of a specific use case?

Comments

Popular posts from this blog

RadioButtons-AGN HUB

Checkbox- AGN HUB

Timepicker- AGN HUB