Chris
Chris

Reputation: 1311

touch event for whole android application

I am developing a new app in which i had a great doubt whether is it possible or not? My doubt is

We have to set one time listener for whole application. And we have to run a set of code for any touch event occurs in our application. For eg. there may be n activities in our app. But we have to set one time listener for all activities and run a set code when any touch occurs in any of n activities. I have tried a lot for it. Any ideas are welcome. Thanks in advance.

Upvotes: 5

Views: 9460

Answers (6)

fiver
fiver

Reputation: 191

The solutions that use the dispatchTouchEvent or onTouch methods don't work for the entire application. Because there are different types of windows. For example, these solutions will not be able to catch touch events if a fragment pops-up or the user clicks a menu because in those cases other things are going on.

Long story short, I was dealing with this problem for my senior project and I developed a library that is able to collect all touch events for an application. The library depends on tricky ways but it also works. The library is open source. You may examine it if you want: https://github.com/fatih-iver/touchy

Just add it as a dependency:

implementation 'fun.observe:touchy:1.0.0'

Then, use it by registering your receiver:

MotionEventBroadcaster.registerReceiver(new MotionEventReceiver() {
    @Override
    protected void onReceive(MotionEvent motionEvent) {
        Log.d(TAG, motionEvent.toString());
    }
});

Upvotes: 0

Dmitry Velychko
Dmitry Velychko

Reputation: 1535

If you whant to use this for whole app you need to create class that extends from ActionBarActivity(or SherlockActivity)

public class MyCustomActivity extends ActionBarActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FrameLayout decorView = (FrameLayout) getActivity().getWindow().getDecorView();
        decorView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.d(TAG, "Click X:" + event.getX() + " Y:" + event.getY());
                return false;
            }
        });
    }
}

and then extends all other Activities from MyCustomActivity:

public class MainActivity extends MyCustomActivity{ }

Than all touch events will go through your TouchListener event

Upvotes: 0

dor506
dor506

Reputation: 5404

I hope I understand you right.

What that you can do is create a class that extends Activity, Lets say TouchActivity. each one of your activities will inherit TouchActivity.

 public class TouchActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) 
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN)
        {
            Log.d("Touch", "Touch");
            sendBroadcast(new Intent("touch_event_has_occured"));
        }

        return super.onTouchEvent(event);
    }
}

now in your global broadcast receiver you will get the brodcast

public class TouchReveiver extends BroadcastReceiver 
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {
        if (intent.getAction().equals("touch_event_has_occured"))
        {
            Log.d("Touch", "Touch");
        }
    }

}

don't forget to register the receiver with the action

Upvotes: 3

st0le
st0le

Reputation: 33545

Totally Possible.

Create a Activity called MyTouchActivity (or something) override the onTouch Event and write the set code you're talking about.

And now, all the rest of the n Activities should extend MyTouchActivity instead of the Regular Activity. This way that code is inherited to all your n Activities. You'll need to create Seperate MyTouchListActivity (for ListActivity) and MyTouchMapActivity (for MapActivity)

The other Method would be to simply create an Ontouch LIstener and then override each onTocuh event in each activity to call your Listener first. But that's cumbersome.

Upvotes: 0

Matthieu
Matthieu

Reputation: 16397

You can create a custom view that's invisible, takes all the screen and uses onTouchEvent to perform whatever operation you need to do.

Then you'll need to have every layout file to include that view on top (probably in a FrameLayout).

Upvotes: 2

Rajesh
Rajesh

Reputation: 15774

What you could do is, create a base activity class that is extended by all other activities in your application. You can then override the public boolean dispatchTouchEvent (MotionEvent ev) method in this base activity. This overridden implementation would be the single place where all your touch events land.

There is one important caveat though, it will work only in case all your activities are of the same type (for example - all of them are Activitiy types or all of them are of type ListActivity)

Upvotes: 1

Related Questions