Search code examples
androidonclicklistenerandroid-constraintlayout

setOnClickListener not getting triggered on ConstraintLayout


I am trying to trigger an OnClick event from the ConstraintLayout defined in xml. But surprisingly it is not getting triggered. Other similar layouts are working and it is exactly the same as others.

Here is the code:

<androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/cl_plan"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@id/plan_txt_subtitle">

                <TextView
                    android:id="@+id/txt_plan_name"
                    style="@style/grocery_item_subtitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="8dp"
                    android:clickable="true"
                    android:focusable="true"
                    app:layout_constraintTop_toTopOf="parent"
                    tools:drawableStartCompat="@drawable/star_icon"
                    tools:text="  Sparkle Plan" />

                <TextView
                    android:id="@+id/txt_upgrade_plan"
                    style="@style/mini_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:clickable="true"
                    android:focusable="true"
                    app:layout_constraintTop_toBottomOf="@id/txt_plan_name"
                    tools:text="@string/upgrade_plan_description" />

                <TextView
                    android:id="@+id/divider_line_0"
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"
                    android:background="@color/black"
                    app:layout_constraintTop_toBottomOf="@id/txt_upgrade_plan" />

            </androidx.constraintlayout.widget.ConstraintLayout>
binding.clPlan.setOnClickListener {
    Toast.makeText(activity, "Clicked", Toast.LENGTH_SHORT).show()
    if (!preferences.isPaidUser())
        findNavController().navigate(R.id.action_accountFragment_to_upgradePlanFragment)
}

I am using data binding and have set it up properly. Other similar layouts with same case working fine, only issue with this one. When I tried to set onclicklistener on TextView txt_plan_name inside it, it works. Any suggestions?


Solution

  • Try removing android:clickable="true" and android:focusable="true" from the text views – they might be intercepting with click listener or may be log or add a toast to check if a click has been registered or not.

    binding.clPlan.setOnClickListener(object : View.OnClickListener {
        override fun onClick(v: View?) {
            Toast.makeText(activity, "Clicked", Toast.LENGTH_SHORT).show()
            if (!preferences.isPaidUser())
                findNavController().navigate(R.id.action_accountFragment_to_upgradePlanFragment)
        }
    })