50

Basically I'd like to attach a single OnClickListener to multiple views inside a ConstraintLayout.

Before migrating to the ConstraintLayout the views where inside one layout onto which I could add a listener. Now they are on the same layer with other views right under the ConstraintLayout.

I tried adding the views to a android.support.constraint.Group and added a OnClickListener to it programmatically.

group.setOnClickListener {
    Log.d("OnClick", "groupClickListener triggered")
}

However this does not seem to work as of the ConstraintLayout version 1.1.0-beta2

Have I done something wrong, is there a way to achieve this behaviour or do I need to attach the listener to each of the single views?

Endzeit
  • 4,810
  • 5
  • 29
  • 52

8 Answers8

75

The Group in ConstraintLayout is just a loose association of views AFAIK. It is not a ViewGroup, so you will not be able to use a single click listener like you did when the views were in a ViewGroup.

As an alternative, you can get a list of ids that are members of your Group in your code and explicitly set the click listener. (I have not found official documentation on this feature, but I believe that it is just lagging the code release.) See documentation on getReferencedIds here.

Java:

    Group group = findViewById(R.id.group);
    int refIds[] = group.getReferencedIds();
    for (int id : refIds) {
        findViewById(id).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // your code here.
            }
        });
    }

In Kotlin you can build an extension function for that.

Kotlin:

    fun Group.setAllOnClickListener(listener: View.OnClickListener?) {
        referencedIds.forEach { id ->
            rootView.findViewById<View>(id).setOnClickListener(listener)
        }
    }

Then call the function on the group:

    group.setAllOnClickListener(View.OnClickListener {
        // code to perform on click event
    })

Update

The referenced ids are not immediately available in 2.0.0-beta2 although they are in 2.0.0-beta1 and before. "Post" the code above to grab the reference ids after layout. Something like this will work.

class MainActivity : AppCompatActivity() {
    fun Group.setAllOnClickListener(listener: View.OnClickListener?) {
        referencedIds.forEach { id ->
            rootView.findViewById<View>(id).setOnClickListener(listener)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Referenced ids are not available here but become available post-layout.
        layout.post {
            group.setAllOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View) {
                    val text = (v as Button).text
                    Toast.makeText(this@MainActivity, text, Toast.LENGTH_SHORT).show()
                }
            })
        }
    }
}

This should work for releases prior to 2.0.0-beta2, so you can just do this and not have to do any version checks.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • 4
    I love extension functions! – Ivan Wooll Mar 15 '18 at 10:51
  • The kotlin extension didn't work for me: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference – Guilherme Lima Pereira Jun 25 '18 at 13:04
  • Be careful doing this - the ConstraintLayout Group is designed to manage _visibility_. I just got bit by this - we were trying to use a group to be able to set a common click listener just like this answer, but also wanted to set the visibility of one of the views outside of the group, and it didn't work. – Jeff Barger Jul 30 '18 at 21:24
  • 2
    Just to look more kotlinish I made a small change `fun Group.setAllOnClickListener(listener: (View) -> Unit) { referencedIds.forEach { id -> rootView.findViewById(id).setOnClickListener(listener) } }` – lucas_sales Oct 05 '18 at 07:16
  • Had an overlapping issue with multiple Constraints Group in different Fragment – toidv Jul 04 '19 at 11:34
  • thanks, @Cheticamp great solutions with kotlin extension function. This is absolutely working with `implementation 'androidx.constraintlayout:constraintlayout:1.1.3'` but if you want to use MotionLayout and Group both using latest `implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'` this is not working. as you can not find referencedIds from group. So if you want to use both MotionLayout and Group use `implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta1'` – Harshad Prajapati Jul 16 '19 at 06:39
  • 2
    One issue with using this approach is that we will end up with non-clickable white spaces between elements of the group, which may result in poor user experience – AntekM Aug 08 '19 at 13:02
  • Has there been an extended functionality of `Group` so that I can only set one click listener to said `Group` as I would to a `ViewGroup`? – Richard Nov 25 '19 at 11:04
  • @Richard not yet AFAIK – reavcn Nov 29 '19 at 16:37
  • @AntekM This might be true in some cases, however it really depends on how the layout is built. If you have multiple referenced ids each one either above or below the others, then you won't end up with non-clickable spaces – reavcn Nov 29 '19 at 16:39
  • Very cool solution. Good call that there will be dead spaces depending on how the layout is built @reavcn – benzabill May 20 '21 at 18:39
  • i used this extention in my home fragment but when i click two times on bottom navigation home icon then this extention is not working – Mohd Naushad Jun 20 '21 at 15:15
20

The better way to listen to click events from multiple views is to add a transparent view as a container on top of all required views. This view has to be at the end (i.e on top) of all the views you need to perform a click on.

Sample container view :

<View
   android:id="@+id/view_container"
   android:layout_width="0dp"
   android:layout_height="0dp"
   app:layout_constraintBottom_toBottomOf="@+id/view_bottom"
   app:layout_constraintEnd_toEndOf="@+id/end_view_guideline"
   app:layout_constraintStart_toStartOf="@+id/start_view_guideline"
   app:layout_constraintTop_toTopOf="parent"/>

Above sample contains all four constraint boundaries within that, we can add views that to listen together and as it is a view, we can do whatever we want, such as ripple effect.

Shashanth
  • 4,995
  • 7
  • 41
  • 51
Vitthalk
  • 354
  • 4
  • 10
  • 1
    or you can put it on the bottom if you want to change the background – Michael Vescovo Feb 12 '18 at 00:31
  • 1
    This works pretty well. You don't need to group your views together. You can even add a selector to the View's background to indicate the user pressed it. – hopia Jun 26 '19 at 00:54
  • Do you have any tip on how to make this view slightly bigger than indicated by constraints? Like add 4dp on each side of the view? – pkuszewski Mar 16 '20 at 15:17
  • I find it the cleanest solution. It is not fancy, and not perfect, but cleaner than one which outvotes the rest. Human-being vote for popular, fancy things, and devs are (sadly) no different. – PrzemekTom May 11 '20 at 10:09
17

To complement the accepted answer for Kotlin users create an extension function and accept a lambda to feel more like the API group.addOnClickListener { }.

Create the extension function:

fun Group.addOnClickListener(listener: (view: View) -> Unit) {
    referencedIds.forEach { id ->
        rootView.findViewById<View>(id).setOnClickListener(listener)
    }
}

usage:

group.addOnClickListener { v ->
    Log.d("GroupExt", v)
}
Rodrigo Queiroz
  • 2,674
  • 24
  • 30
6

The extension method is great but you can make it even better by changing it to

fun Group.setAllOnClickListener(listener: (View) -> Unit) {
    referencedIds.forEach { id ->
        rootView.findViewById<View>(id).setOnClickListener(listener)
    }
}

So the calling would be like this

group.setAllOnClickListener {
    // code to perform on click event
}

Now the need for explicitly defining View.OnClickListener is now gone.

You can also define your own interface for GroupOnClickLitener like this

interface GroupOnClickListener {
    fun onClick(group: Group)
}

and then define an extension method like this

fun Group.setAllOnClickListener(listener: GroupOnClickListener) {
    referencedIds.forEach { id ->
        rootView.findViewById<View>(id).setOnClickListener { listener.onClick(this)}
    }
}

and use it like this

groupOne.setAllOnClickListener(this)
groupTwo.setAllOnClickListener(this)
groupThree.setAllOnClickListener(this)

override fun onClick(group: Group) {
    when(group.id){
        R.id.group1 -> //code for group1
        R.id.group2 -> //code for group2
        R.id.group3 -> //code for group3
        else -> throw IllegalArgumentException("wrong group id")
    }
}

The second approach has a better performance if the number of views is large since you only use one object as a listener for all the views!

Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67
  • "The second approach has a better performance" - But your "one object" is 3 times the size because it contains all the different listeners. Surely if the listeners are different it makes sense to set 3 different objects; and if they're the same, you can still declare one listener in the first case and pass it to all 3 setters: `val listener: (View -> Unit) = {...}` – charles-allen Dec 04 '20 at 04:16
3

While I like the general approach in Vitthalk's answer I think it has one major drawback and two minor ones.

  1. It does not account for dynamic position changes of the single views

  2. It may register clicks for views that are not part of the group

  3. It is not a generic solution to this rather common problem

While I'm not sure about a solution to the second point, there clearly are quite easy ones to the first and third.


1. Accounting position changes of element in the group

This is actually rather simple. One can use the toolset of the constraint layout to adjust the edges of the transparent view. We simply use Barriers to receive the leftmost, rightmost etc. positions of any View in the group. Then we can adjust the transparent view to the barriers instead of concrete views.

3. Generic solution

Using Kotlin we can extend the Group-Class to include a method that adds a ClickListener onto a View as described above. This method simply adds the Barriers to the layout paying attention to every child of the group, the transparent view that is aligned to the barriers and registers the ClickListener to the latter one.

This way we simply need to call the method on the Group and do not need to add the views to the layout manually everytime we need this behaviour.

Endzeit
  • 4,810
  • 5
  • 29
  • 52
3

in Constraintlayout 2.0.0,you can use Layer to resolve multiple views click event,and also support scale animation

2

For the Java people out there like me:

public class MyConstraintLayoutGroup extends Group {
    public MyConstraintLayoutGroup(Context context) {
        super(context);
    }

    public MyConstraintLayoutGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyConstraintLayoutGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setOnClickListener(OnClickListener listener) {
        for (int id : getReferencedIds()) {
            getRootView().findViewById(id).setOnClickListener(listener);
        }
    }
}

This is not propagating click states to all other children however.

nilsi
  • 10,351
  • 10
  • 67
  • 79
0
fun ConstraintLayout.setAllOnClickListener(listener: (View) -> Unit) {
    children.forEach { view ->
        rootView.findViewById<View>(view.id).setOnClickListener { listener.invoke(this) }
    }
}

And then

.setAllOnClickListener {
                do something
            }
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841