Jumpa
Jumpa

Reputation: 4409

AutocompleteTextView click listener called twice

I've this layout:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/input_birthdate"
    style="@style/ExposedDropDownMenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/birthdate">

    <AutoCompleteTextView
        android:id="@+id/autocomplete_birthdate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:editable="false" />

</com.google.android.material.textfield.TextInputLayout>

With this style:

<!-- ExposedDropdownMenu -->
<style name="ExposedDropDownMenu" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
    <item name="boxStrokeColor">@color/text_input_layout_outlined_box_stroke</item>
    <item name="hintTextColor">@color/green_2</item>
</style>

I've tried to set a click listener on the autocomplete text view:

autoComplete.setOnClickListener(view -> {
    // This is called twice when i click the autocomplete textview
});

The listener is called twice...why? How can i solve this?

EDIT: removing the style, the first click (gaining the focus) is ignored, than the second click is called correctly once, but I lose the "ripple" background effect anyway.

Upvotes: 6

Views: 1863

Answers (2)

Fatehali Asamadi
Fatehali Asamadi

Reputation: 226

You should try the below example.

<com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Click Event"
            app:boxStrokeColor="@color/til_selector"
            app:boxStrokeErrorColor="@color/colorRed_700"
            app:boxStrokeWidth="1dp">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/etClickEvent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:maxLines="1"
                **android:focusableInTouchMode="false"** />

        </com.google.android.material.textfield.TextInputLayout>

Now set Programmatically

etClickEvent.inputType = 0
etClickEvent.setOnClickListener{
//PUT YOUR OWN 
}

This is working fine for me.

Upvotes: 0

Zakaria
Zakaria

Reputation: 589

You can try the setOnTouchListener instead, it's called just once and keeps the ripple as well.

autocomplete_birthdate.setOnTouchListener { view, motionEvent ->
    if (motionEvent.action == ACTION_UP) {
        // Some code
    }
    return false
}

Upvotes: 1

Related Questions