EventListeners: onClickListeners, onClick

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.

Similary,

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.

ViewCLickListener

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

  • View.OnClickListener
  • View.OnLongClickListener
  • View.OnFocusChangeListener
  • View.OnKeyListener
  • View.OnTouchListener
  • View.OnCreateContextMenuListener

You Can Create Event Listener in 2 ways.

  1. through XML
  2. through Code.

through XML

  • Declare function name in XML View.
  • Define function in Activity.
android:onClick="myCustomeListener" // .XML layout file
public void myCustomListener(View view){ // .class Activity file
    customBehaviour();
}

through Code

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

Method 1####

TextView tv1 = (TextView) findViewById(R.id.screen2Item);

CustomClickListener secClickList = new CustomClickListener(SecondActivity.class);
tv1.setOnClickListener(secClickList);
		
//CustomClickListener.class

public class CustomClickListener implements View.OnClickListener {

 @Override
 public void onClick(View v) {
 Intent intent = new Intent(v.getContext(), SecondScreenActivity.class);
 v.getContext().startActivity(intent);
 }

 //SecondScreenActivity.class
  setContentView(R.layout.activity_second); // This will inflate/Show the Activity.

If you have more than one View to Click and more than one Activity to display, you could modify CustomClickListener.class.

Method 2####

TextView tv1 = (TextView) findViewById(R.id.screen2Item);
TextView tv2 = (TextView) findViewById(R.id.screen3Item);

CustomClickListener secClickList = new CustomClickListener(SecondScreenActivity.class);
tv1.setOnClickListener(secClickList);

CustomClickListener secClickList = new CustomClickListener(ThirdScreenActivity.class);
tv2.setOnClickListener(secClickList);
		
//CustomClickListener.class

public class CustomClickListener implements View.OnClickListener {
    Class<?> clss;

    public SecondScreenClickListener(Class<?> clss) {
        this.clss = clss;
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(v.getContext(), clss);
        v.getContext().startActivity(intent);
    }

//xxxxScreenActivity.class

setContentView(R.layout.activity_second); // This will inflate/Show the Activity.

Method 3####

TextView tv1 = (TextView) findViewById(R.id.screen2Item);

To make it much cleaner,

CustomClickListener secClickList = new CustomClickListener(SecondActivity.class);
tv1.setOnClickListener(secClickList);

can be written as,

secClickList in the above line, can be replaced directly.
// i.e., tv1.setOnClickListener(new CustomClickListener());
//Here CustomClickListener implements View.OnClickListener, which can be used directly.

tv1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
});

and delete CustomClickListener.class

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.

You might also enjoy