How to create our own Listener interface in android?


Could someone help me to create user defined listener interface with some code snippets?

3/12/2015 9:58:26 PM

Create a new file:

public interface MyListener {
    // you can define any parameter as per your requirement
    public void callback(View view, String result);

In your activity, implement the interface:

public class MyActivity extends Activity implements MyListener {
   public void onCreate(){
        MyButton m = new MyButton(this);

    // method is invoked when MyButton is clicked
    public void callback(View view, String result) {   
        // do your stuff here

In your custom class, invoke the interface when needed:

public class MyButton {
    MyListener ml;

    // constructor
    MyButton(MyListener ml) {
        //Setting the listener = ml;

    public void MyLogicToIntimateOthers() {
        //Invoke the interface
        ml.callback(this, "success");
7/31/2018 8:53:59 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow