1

so i started to learn Gesture, everything work fine, and then i changed this(context of activity) to one button in activity(button.getContext()). I though it will catch event in only button but it catch event on all the screen view like it used to be. Can you guys explain me why cause i only knows that context is like a status of object or activity, thanks for your time

  detector = new GestureDetector(btn.getContext(), new GestureDetector.OnGestureListener(){};
Tú Anh Dư
  • 135
  • 1
  • 10
  • Very simplified you can say that a `Context` is the link to the process executing your code. Based on this information many more information can be derived, the APK with it's resources inside and many more. – Robert Apr 06 '17 at 18:21

3 Answers3

1

Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.

Source

Oğuzhan Döngül
  • 7,856
  • 4
  • 38
  • 52
1

In your scenario, GestureDetector needs a Context of an Activity. Either you pass MyCurrentActivity.this as a reference or button.getContext(), both contexts belong to same Activity.

Context which you are getting from button.getContext() is originally set when view is inflated.

Rahul
  • 10,457
  • 4
  • 35
  • 55
1

The context is basically access to the application resources. When you get the context of the button, you are really getting a reference to the context of the activity that the button sits in, not the button itself. There are 4 types of Context in Android:

  1. Application Context
  2. Activity/Service Context
  3. Broadcast Receiver
  4. Content Provider

Each of these Context types has different responsibilities and available resources. So the context usage here isn't to limit where you can touch, but rather that you can work with the UI. If you want to limit where the gesture detector is working, you would simply attach the detector to the view. You would still need the Activity context to create the detector though.

I found this article on the different context types to be very helpful:

https://possiblemobile.com/2013/06/context/

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156