'How do I add setOnLongClickListener on an imageview in recyclerview

I'm using holder.itemView.setOnLongClickListener in Kotlin but you can long press anywhere in the red rectangle that I have drawn. screenshot 1

I want it so you have to long press the image.

screenshot 2

In my adapter class:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    val currentMessage = messageList[position]

    holder.itemView.setOnLongClickListener {
        
        // Code runs here after long press            
        // currentMessage is an object containing few things about the message that you have long pressed

        true
    }
}


Solution 1:[1]

I figured it out myself.

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val currentMessage = messageList[position]

holder.itemView.findViewById<ImageView>(R.id.image_id).setOnLongClickListener {
    
    // Code runs here after long press            
    // currentMessage is an object containing few things about the message that you have long pressed

    true
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1