Toshiro007
Toshiro007

Reputation: 77

AutoCompleteTextView with search ime options

I want to do the searching 2 ways with autocompletetextview. The first one is the basic one which uses ArrayAdapter (This one works fine), the second one is using IME_OPTION_SEARCH or actionSearch. I want to discuss the second one which is the problem here.

Basically, I need to add imeOptions to autocompletetextview with "actionSearch" and it would be like this:

<AutoCompleteTextView
            android:layout_width="0dp"
            android:layout_height="wrap_content" android:id="@+id/acPetShopSearch"
            app:layout_constraintStart_toEndOf="@+id/ivPetShopSearchIcon" android:layout_marginStart="8dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent" android:backgroundTint="@android:color/transparent"
            android:hint="@string/pet_shop_search_hint"
            android:imeOptions="actionSearch"
            android:inputType="textAutoComplete|textAutoCorrect"
            app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="16dp"
            android:textColorHint="@color/pinkish_grey" android:textColor="@color/purple_brown"
            android:textSize="12sp" android:singleLine="true"/>

Then, I set editor listener to the view like simple EditText with actionSearch, so it would be like this:

acPetShopSearch.setOnEditorActionListener(object : TextView.OnEditorActionListener {
        override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent): Boolean {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                doSearch(acPetShopSearch.text.toString())
                return true
            }
            return false
        }
    })

I tried this and I got the error like this:

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter event at id.paw.pawcolony.view.petshop.PetShopActivity$successCity$1.onEditorAction(Unknown Source:7) at android.widget.TextView.onEditorAction(TextView.java:6271) at com.android.internal.widget.EditableInputConnection.performEditorAction(EditableInputConnection.java:138) at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:360) at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:201) at android.app.ActivityThread.main(ActivityThread.java:6806) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

Is the AutoCompleteTextView's behavior is different from EditText? Is there any way to put search feature to AutoCompleteTextView?

Upvotes: 2

Views: 758

Answers (1)

Ogan Belema
Ogan Belema

Reputation: 11

acPetShopSearch.setOnEditorActionListener(object : TextView.OnEditorActionListener {
        override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?): Boolean {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                doSearch(acPetShopSearch.text.toString())
                return true
            }
            return false
        }
    })

Fixed mine by making KeyEvent nullable

Upvotes: 1

Related Questions