Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnClickListener interface in Android

Tags:

java

android

I am new to programming, I have very basic knowledge in android. As I learned in java that an Interface cannot be instantiated And that new is a keyword in Java that indicates creating an instance. I came across following code in Android:

public class MyActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

    setContentView(R.layout.mylayout);
    findViewById(R.id.button1).setOnClickListener(mButton1_OnClickListener);
  }  

  //On click listener for button1
  final OnClickListener mButton1_OnClickListener = new OnClickListener() {
    public void onClick(final View v) {
        //Inform the user the button has been clicked
        Toast.makeText(this, "Button1 clicked.", Toast.LENGTH_SHORT).show();               
    }
  };
}

Above code OnClickListener is a public interface and onClick(final View v) is an abstract method, Here goes my question: OnClickListener being an interface, how is possible to create an instance of it by using the new keyword in the example above?

like image 313
Pradeep kumar AR Avatar asked Dec 26 '13 06:12

Pradeep kumar AR


People also ask

What is onclicklistener in Android?

When you define a listener using the onClick attribute, the view looks for a method with that name only in its host activity. Programmatically setting an OnClickListener allows you to control a button’s behavior from somewhere other than its host activity. What is onCreate method in Android?

Is onclicklistener an interface or abstract method?

Above code OnClickListener is a public interface and onClick (final View v) is an abstract method, Here goes my question: OnClickListener being an interface, how is possible to create an instance of it by using the new keyword in the example above? Show activity on this post.

How do I get the onclick method in Android?

Define the onClick method in main.xml You can also define the onClick method in main.xml when declaring the button (or any other clickable) component. When doing so, you have to declare the method you want as the onClick method. For example, as you can see, I’ve added an android:onClick attribute with value clickFunc.

What is an event listener in Android?

An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI. What are callbacks in Android?


1 Answers

Here you are not instantiating an interface you are doing by Anonymous inner class.Your are not creating the object of the interface .you are creating this for the Anonymous class which is you gonna create in next step inside {Anonymous inner class};.If want to know more details of this you have to through Anonymous inner class.

Check it out below links:

http://c2.com/cgi/wiki?AnonymousInnerClass

http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html

like image 175
rupesh Avatar answered Sep 18 '22 02:09

rupesh