0

I am newbie in android and learning first time.

I am making a very simple snippet something like when user clicks the button, the app should show a Toast message.

I have read many tutorials on the web. All tutorials I have seen on the web uses anonymous inner class to implement listeners and their methods.

What I have tried is here:

public class MainActivity extends Activity {

    private Button mTrue;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTrue= (Button) findViewById(R.id.turebutton);


        mTrue.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, R.string.correct_toast,Toast.LENGTH_LONG).show();

            }
        });

}

It works perfectly, but seems difficult in understanding.

I am familiar with Java basics. i have read this and I already know about inner class and anonymous inner class in java.

In java,if we code something like this sample,

public void init(){
      addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent me){
                       showStatus("mouse pressed");
             }
      });
}

the MouseListener is an interface and then we make instance of MouseAdapter and pass it to call Listener's method on it.

I have some questions:

  1. Why we have passed the instance of interface (View.onClickListener) here?..i have checked the documentation, View.onClickListener is an interface not the class.

  2. setonClickListener is looking same like addMouseListener. but the conflicting thing is In java, we had to add Listeners and pass instances of class. here things are inverse.

I am totally wired. please help if anyone can explain well. thanks in advance.

5
  • do you want to know why do is View. added before OnClickListener? Commented Aug 20, 2014 at 14:44
  • @blackbelt yes and also i want to know about why things are different in android than we code in Java?(only in the context of my question) Commented Aug 20, 2014 at 14:47
  • 1. not instance of interface but instance of anonymous class(in-place interface implementation) 2. not java but awt ... it doesn't matter ... unless you need class that should extend 2 other classes ... it is not possible in java ... thats why interfaces are (IMHO) better in such places(simple UI events) Commented Aug 20, 2014 at 14:48
  • @Selvin can you please explain better in answer? Commented Aug 20, 2014 at 14:50
  • in both cases you are passing anonymous class instance: in first example this class is implementing interface, in second extends class ... anyway with such code grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/… MouseAdapter could be interface, too Commented Aug 20, 2014 at 15:01

2 Answers 2

2
    mTrue.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Toast.makeText(MainActivity.this, R.string.correct_toast,Toast.LENGTH_LONG).show();

        }
    });

Here you are passing an instance of an anonymous class implementing the View.OnClickListener() interface.

The setOnClickListener method of View class accepts a parameter of type View.OnClickListener. Therefore, giving an instance that implements this interface works.

As for addMouseListener, assuming you are referring to this method of Component, it accepts an interface too - MouseListener.

However, in your example :

public void init(){
      addMouseListener(new MouseAdapter(){
                public void mousePressed(MouseEvent me){
                       showStatus("mouse pressed");
             }
      });
}

you are passing to it an instance of an anonymous class that inherits from MouseAdapter, which itself implements the interface MouseListener.

Those two examples are therefore quite similar.

Sign up to request clarification or add additional context in comments.

3 Comments

it is a method of View not of Button
How come this answer got a down vote just 5 seconds after it was posted?. Downvoter please explain yourself.
@blackbelt My mistake. I saw this method in the class reference of Button and missed the fact that it's inherited from View.
1

Why we have passed the instance of interface (View.onClickListener) here?..i have checked the documentation, View.onClickListener is an interface not the class.

Here, new View.OnClickListener() {.. means you am creating an instance of an anonymous class which implements View.OnClickListener interface.

setOnClickListener is looking same like addMouseListener. but the conflicting thing is In java, we had to add Listeners and pass instances of class. here thing are inverse.

Here you are adding a click listener to the button mTrue. check- mTrue.setOnClickListener. The setOnClickListener() sets the listener to your button

2 Comments

ok..thanks for first answer..i understood it.but i can't understand the second answer. setOnClickListener is the method of View class...Should it not be a method of listener interface?
No, setOnClickListener is a method of you Button here, and it's logical since the button has to be observable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.