setOnClickListener doesn't work for Image Slider in Kotlin

648 views Asked by At

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()
    
            }
}
3

There are 3 answers

0
Codist On BEST ANSWER

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.

0
Felipe Palma On

I think that you must add in your imageSlider setItemOnClickListener method. Add this code in your onCreate imageSlide.setItemOnClickListener()

0
Kigige Michael On

This gave me a problem for a while. My concern was a click listener on the image slider (not a specific image). I worked around it by setting up a transparent button appear on top of the image slider using CoordinatorLayout.

<androidx.coordinatorlayout.widget.CoordinatorLayout
                    android:layout_width="match_parent"
                    android:layout_height="180dp">

                    <com.denzcoskun.imageslider.ImageSlider
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        app:iss_auto_cycle="true"
                        app:iss_period="3300"
                        app:iss_delay="0"
                        android:id="@+id/image_slider"/>

                    <Button
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="@android:color/transparent"
                        android:id="@+id/button_trans"/>
                </androidx.coordinatorlayout.widget.CoordinatorLayout>

Then i set up an onclick listener for the button.