How To Fire An Event When Someone Clicks Anywhere On The Screen In An Android App?
Solution 1:
setonclicklistner
for main layout of your layout file....
Like....main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/mainlayout">
<!- Your other view->
</Relativelayout>
and set click listener for mainlayout...
RelativeLayoutrlayout= (RelativeLayout) findViewById(R.id.mainlayout);
rlayout.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v) {
}
});
Solution 2:
I believe the easiest way is to override onUserInteraction in your Activity
Just be aware that it won't fire when users focus on/off EditText
s and Spinners
(probably other Widgets too). But it will fire every time the user touches the screen or presses a Button.
But this makes it unnecessary to build listeners into your layouts or write additional methods to handle those listeners, so I believe it's the most trouble-free way to get what you want.
Solution 3:
You should just override Activity
's dispatchTouchEvent(ev: MotionEvent)
method
Look at this example in Kotlin. This approach will not affect any onClickListenters in your app
/**
* On each touch event:
* Check is [snackbar] present and displayed
* and dismiss it if user touched anywhere outside it's bounds
*/overridefundispatchTouchEvent(ev: MotionEvent): Boolean {
// dismiss shown snackbar if user tapped anywhere outside snackbar
snackbar?.takeIf { it.isShown }?.run {
val touchPoint = Point(Math.round(ev.rawX), Math.round(ev.rawY))
if (!isPointInsideViewBounds(view, touchPoint)) {
dismiss()
snackbar = null// set snackbar to null to prevent this block being executed twice
}
}
// call superreturnsuper.dispatchTouchEvent(ev)
}
Or check out the full gist
Solution 4:
If you want to know, that the user clicked anywhere on the screen then the trick is to override OnUserUnteraction():
"Called whenever a key, touch, or trackball event is dispatched to the activity.Implement this method if you wish to know that the user has interacted with the device in some way while your activity is running. This callback and {@link #onUserLeaveHint} are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notification...."
publicabstractclassBaseActivityextendsActivity {
...
@OverridepublicvoidonUserInteraction() {
Log.d(DEBUG_TAG,"Touch anywhere happened");
super.onUserInteraction();
}
I am using it to set up app screen block after some inactivity.
Solution 5:
Give your main layout an id, then
LinearLayout root = (LinearLayout) findViewById(R.id.main_layout);
root.setOnClickListener(new OnClickListener() {
publicvoidonClick()
{
}
});
Post a Comment for "How To Fire An Event When Someone Clicks Anywhere On The Screen In An Android App?"