In our real word, consider different type of protection forces (City, Airport, Railway, Border etc). If some crime/event happens specific type of force are notified, and the type of action that is taken differs from event-event.
Here, protection forces are Event Listeners.
Consider we have Screen 1 presented to a User. User may start interacting by touching/clicking the View items (i.e., Button/TexView/any). User could also double touch, long touch. These Clicks are events that are notified (by the Screen Hardware) to the Android Framework, and Android will take of detecting from which View these events are generated. As a developer, if you want to perform some action on a View when any of these events occur, you could do so by using Interfaces, called Event Listeners.
Views have a collection of Interfaces with callback functions, (functions that are called by the Framework when an Event occurs) which developer can use to define custom actions.
Some of the Interfaces that are available are
You Can Create Event Listener in 2 ways.
- through XML
- through Code.
- Declare function name in XML View.
- Define function in Activity.
- Set View ID in XML.
- Get View handle by findViewById.
- Create CustomClickListener by inheriting View.OnClickListener Listener.
- Create Listener Object.
- Attach Listener Object to View.
If you have more than one View to Click and more than one Activity to display, you could modify CustomClickListener.class.
TextView tv1 = (TextView) findViewById(R.id.screen2Item);
To make it much cleaner,
can be written as,
When to use Code: If you use Fragments. If a View has to call for different funciton at run time.
When to use XML: If you do not re-use the View(s) for more than one behaviour.
Other difference is explained here
Usage of Class<?> can be found here
References: Udacity, Android Developer Document, Code Path, StackOverflow.