setOnClickListener doesn’t work for Image Slider in Kotlin

Issue

I have been trying to set a click listener for com.denzcoskun.imageslider.ImageSlider but it doesn’t work. The image slider works perfect, it shows the slideshow. The only issue is that it doesn’t get clicked.

Following the XML code of the image slider.

            <com.denzcoskun.imageslider.ImageSlider
                android:id"@+id/image_slider"
                android:layout_width"match_parent"
                android:layout_height"200dp"
                android:clickable"true"
                android:focusable"true"
                android:minHeight"@dimen/item_dashboard_image_height"
                app:iss_auto_cycle"true"
                app:iss_corner_radius"5"
                app:iss_delay"0"
                app:iss_error_image"@color/colorDarkGrey"
                app:iss_period"2500"
                app:iss_placeholder"@color/colorDarkGrey"
                app:iss_selected_dot"@drawable/default_selected_dot"
                app:iss_unselected_dot"@drawable/default_unselected_dot"
                app:layout_constraintEnd_toEndOf"parent"
                app:layout_constraintStart_toStartOf"parent"
                app:layout_constraintTop_toTopOf"parent" />

Following is the Kotlin code. This is within my onCreate() method. It doesn’t show the toast message. Where is the mistake?

    class DetailsActivity : BaseActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)

            binding  ActivityProductDetailsBinding.inflate(layoutInflater)
            setContentView(binding.root)

            binding.imageSlider.setOnClickListener {

                Toast.makeText(this,"clicked",Toast.LENGTH_SHORT).show()

            }
}

Solution

The issue is that I used the clickListener in the onCreate(). The solution is that I have to use the ClickListener after binding the data rather than using it in the onCreate() method. It’s now working fine.

Answered By – Codist

Leave a Comment