Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 491
Jumpa Avatar asked Feb 04 '20 11:02

Jumpa


1 Answers

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
}
like image 75
ZakariaBK Avatar answered Nov 10 '22 01:11

ZakariaBK