LinearLayout layout = findViewById(R.id.layout); layout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Do something on click event } });
RelativeLayout layout = findViewById(R.id.layout); layout.setOnClickListener(v -> { // Do something on click event });This example is similar to the previous example, but we are using a lambda expression in place of anonymous inner class for the onClick() method. These methods belong to the android.view.ViewGroup class which is located in the android.view package.