Group at ConstraintLayout is just a weak association of AFAIK views. This is not a ViewGroup , so you cannot use the one-click listener, as you did when the views were in the ViewGroup .
Alternatively, you can get a list of identifiers that are members of your Group in your code and explicitly set a click listener. (I did not find the official documentation for this function, but I believe that it is simply behind the release of the code.) See the documentation for 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) {
In Kotlin, you can create an extension function for this.
Kotlin:
fun Group.setAllOnClickListener(listener: View.OnClickListener?) { referencedIds.forEach { id -> rootView.findViewById<View>(id).setOnClickListener(listener) } }
Then call the function for the group:
group.setAllOnClickListener(View.OnClickListener {
Refresh
These identifiers are not immediately available in 2.0.0-beta2, although they are in 2.0.0-beta1 and earlier. "Post" the above code to get reference identifiers after posting. 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)
This should work for releases prior to 2.0.0-beta2, so you can just do it and not do any version checks.
source share