Event Listener and Callback method in Android

In android an event listener is an interface which is having a callback method which executed when user triggered some event or action like button click, long click, focus etc.

Example of Event Listener

For this example I am taking a textview, textbox and a button. When I will click on button I want to display textbox data in textview.

 
		<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/t1" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_below="@+id/t1"
    android:layout_toRightOf="@+id/t1"
    android:layout_toEndOf="@+id/t1" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="ShowData"
    android:id="@+id/b1"
    android:layout_below="@+id/editText"
    android:layout_toRightOf="@+id/t1"
    android:layout_toEndOf="@+id/t1"
    android:layout_marginTop="62dp" />

		

In onCreate Method first of all we will access the button then will create the listener and callback method

 
		Button button = (Button)findViewById(R.id.b1);

Event Listner
button.setOnClickListener(
        new Button.OnClickListener(){


            //callback methods
            public void onClick(View V)
            {
                EditText e1=(EditText)findViewById(R.id.editText);
                android.widget.TextView tv=(android.widget.TextView)findViewById(R.id.t1);
                tv.setText(e1.getText());


            }

        }


);


Now execute the code and you will get following output.

event listener