Home > OS >  Text field dropdown menu in Kotlin isn't working
Text field dropdown menu in Kotlin isn't working

Time:12-20

Here is my XML code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    >
    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        >
    </com.google.android.material.textfield.TextInputLayout>
    <AutoCompleteTextView
        android:id="@ id/auto_complete_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="none"
        android:hint="@string/select"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
        />



</androidx.constraintlayout.widget.ConstraintLayout>

Here is my .kt file

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
     super.onViewCreated(view, savedInstanceState)
     val autocompleteTextView = view.findViewById<AutoCompleteTextView>(R.id.auto_complete_txt)
//     System.out.println(autocompleteTextView)
     val items = arrayOf("Test","Test1","Test2","Test3","Test4","Test5","Test6")
        val adapter  = ArrayAdapter(requireContext(),android.R.layout.simple_list_item_1,items)
        autocompleteTextView.setAdapter(adapter)
//                autocompleteTextView.setOnItemClickListener(AdapterView.OnItemClickListener())
    }

I try to do following this example https://www.youtube.com/watch?v=EBhmRaa8nhE

Any suggestions for mentioned issue is highly appreciated.

CodePudding user response:

Because your AutoCompleteTextView is not inside the TextInputLayout. You've to write like this.

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="16dp">

 <AutoCompleteTextView
    android:id="@ id/auto_complete_txt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="none"
    android:hint="@string/select"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
    />
</com.google.android.material.textfield.TextInputLayout>
  • Related