Search code examples
javaandroidandroid-layoutimageviewtouch

How to detect click on empty space?


I'm trying to create an activity that lets me place and move imageviews around. To create new imageviews I wanted to tap on an empty space. However, I can't see how I can implement that. There isn't a specific object I can set an OnClickListener to. Unless then I'd set the root layout as to the OnClickListener but then how am I supposed to check that it's empty space?

In short, how do I detect when the user touches an empty space?


Solution

  • You should set a listener on a view that contains your images (holder). If it's rootView - that's completely fine. The thing is that onClick will work on direct interaction with that view, which basically means if you click on other views within your holder (children/item overlapping holder), nothing should happen.

    You might need to set your image views clickable by calling imageView.setClickable(true) and you will not get rootView's false click. Related: Android root view onClickListener is always triggered

    So to sum up: you should not check empty space by yourself, system does it for you