gurehbgui
gurehbgui

Reputation: 14694

how to fire an event when someone clicks anywhere on the screen in an android app?

how can I catch the event when click occurs somewhere on the app screen? It doesn't matter if there is a button or something else. I just need to apply an onClick() event listener on the whole application screen.
How to do this?

Upvotes: 36

Views: 57948

Answers (7)

Alex Burov
Alex Burov

Reputation: 1951

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...."

public abstract class BaseActivity extends Activity {
...
@Override
public void onUserInteraction() {
    Log.d(DEBUG_TAG,"Touch anywhere happened");
    super.onUserInteraction();
}

I am using it to set up app screen block after some inactivity.

Upvotes: 12

Andrey Busik
Andrey Busik

Reputation: 441

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
 */
override fun dispatchTouchEvent(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 super
    return super.dispatchTouchEvent(ev)
}

Or check out the full gist

Upvotes: 13

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28379

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 EditTexts 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.

Upvotes: 11

Antzi
Antzi

Reputation: 13444

You can also directly set a callback from the onClick xml attribute of the root layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns: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:onClick"myMethod">
 <!- Your other view->
</Relativelayout>

And in your activity:

public void myMethod(View pView) {
  //XXX do something
}

Less bloated code this way :)

Upvotes: 1

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

setonclicklistner for main layout of your layout file....

Like....main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns: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...

 RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.mainlayout);
 rlayout.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

    }

 });

Upvotes: 30

DGomez
DGomez

Reputation: 1480

Maybe you can add a listener to the Layout instance, getting the layout with

RelativeLayout l = (RelativeLayout)findViewById(R.id.mylayout);
l.setOnClickListener(click);

Upvotes: 0

Manitoba
Manitoba

Reputation: 8702

Give your main layout an id, then

LinearLayout root = (LinearLayout) findViewById(R.id.main_layout);
root.setOnClickListener(new OnClickListener() {
    public void onClick()
    {

    }
});

Upvotes: 1

Related Questions