Anil Gorthy
Anil Gorthy

Reputation: 899

Button onclicklistener not fired in Fragment

I'm using Kotlin 1.3.61, kotlin extensions, androidx.appcompat:appcompat:1.1.0, androidx.constraintlayout:constraintlayout:1.1.3, androidx.core:core-ktx:1.1.0, org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.61

and access a button defined in my xml in the fragment, like so:

class MyFragment : Fragment(), OnMapReadyCallback {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.i(TAG, "In onCreate()")
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        Log.i(TAG, "In onCreateView()")
        return inflater.inflate(R.layout.fragment_map_bottom_view, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        ...
        whereBtn.setOnClickListener {
            Log.i(TAG, "where clicked but first click doesn't register") 
        }

but the first click of the button is never fired but the second and subsequent clicks do fire.

I tried @ContainerOptions(CacheImplementation.SPARSE_ARRAY) for the fragment but no luck.

Here is the fragment layout file:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:backgroundTint="@color/white">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/headerTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="12dp"
        android:layout_marginEnd="16dp"
        android:text="@string/navHeaderTitle"
        android:textAppearance="@style/ChipTextCaptionFont"
        android:textColor="@color/black"
        app:layout_constraintBottom_toTopOf="@+id/whereBtn"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.095"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/whereBtn"
        style="@style/DrawerMenuItemsFont"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_below="@+id/headerTitle"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="32dp"
        android:paddingStart="12dp"
        android:paddingEnd="12dp"
        android:drawablePadding="10dp"
        android:drawableStart="@drawable/ic_search_24px"
        android:focusableInTouchMode="true"
        android:gravity="center_vertical"
        android:hint="@string/where_desc"
        android:clickable="true"
        android:focusable="true"
        android:textAppearance="@style/DrawerProfileCaptionFont"
        android:textSize="16sp"
        android:textColorHint="@color/black"
        android:background="@drawable/rect"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.285"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/headerTitle" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 66

Answers (1)

Choim
Choim

Reputation: 392

it seems that whereBtn is focusable.

There is no event to perform click at first time because focus change already has consumed the event.

Upvotes: 1

Related Questions