1

I created a compounded custom View using ConstraintLayout.

class CustomButtonView(
    context: Context,
    attrs: AttributeSet?
) : ConstraintLayout(context, attrs) {
    constructor(context: Context) : this(context, null)

    init {
        val inflater = context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        inflater.inflate(R.layout.custom_button_view, this, true)
        super.setOnClickListener{
            Timber.d("CLICKED")
        }
        this.setOnClickListener {
            Timber.d("CLICKED")
        }
        rootView.setOnClickListener {
            Timber.d("CLICKED")
        }
    }
}

The custom_button_view xml is:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/icon_camera"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:src="@drawable/icon_camera" />

    <TextView
        style="@style/ButtonText"
        android:id="@+id/button_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/text_drawable_margin"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@id/icon_camera"
        android:text="@string/button_text" />

    <ImageView
        android:id="@+id/icon_dot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/text_drawable_margin"
        android:layout_marginTop="@dimen/small_margin"
        app:layout_constraintTop_toTopOf="@id/button_text"
        app:layout_constraintBottom_toBottomOf="@id/button_text"
        app:layout_constraintStart_toEndOf="@id/button_text"
        android:src="@drawable/icon_dot" />

</merge>

And when I include it in a layout I just do:

        <com.xxx.etc..CustomButtonView
            android:id="@+id/custom_button"
            style="@style/CustomButton"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

I used to do this often in the past but I don't remember how I was making the click working. I have tried to set the click listener on the view as well:

binding.customButton.setOnClickListener {
    Timber.d("CLICKED")
}

None of the clicks are working.

1048576
  • 615
  • 9
  • 27
  • Maybe you need to call `setClickable(true)` in your custom view to make it clickable? – Ocasin May 05 '22 at 09:00
  • @Ocasin I tried it and it didn't help and they I have even found that there are cases when it would just confuse and lead to bugs if you set the clickable on the wrong view – 1048576 May 05 '22 at 09:58

1 Answers1

0

I solved adding:

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_UP) {
            Timber.d("CLICKED")
        }
        return super.dispatchTouchEvent(event)
    }


    override fun dispatchKeyEvent(event: KeyEvent): Boolean {
        if (event.action == KeyEvent.ACTION_UP && (event.keyCode == KeyEvent.KEYCODE_DPAD_CENTER || event.keyCode == KeyEvent.KEYCODE_ENTER)) {
            Timber.d("CLICKED")
        }
        return super.dispatchKeyEvent(event)
    }

taken from How do I get the OnClickListener working on a custom view with a RelativeLayout?

1048576
  • 615
  • 9
  • 27