-2

I'm new to Android environment I'm just wondering why does the OnClick(View v) method accepts this if the required parameter is in type View and my method extends Activity? Is there a relationship between View and Activity? Please refer to the code to make the question clearer.

...
public class MainActivity extends Activity implements OnClickListener {

    private Button btn1;
    private Button btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        ...
        btn1 = (Button)findViewById(R.id.button1);
        btn2 = (Button)findViewById(R.id.button2);

        // Below are the methods calling which confuses me
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        ...
    }

    public void onClick(View v) {
        int id = v.get();
        switch(id) {
            case R.id.button1:
                //statement for button1
            case R.id.button2:
                //statement for button2
        ...
        }
    }
}

EDIT:This question is misleading, just caused by overlooking 'setOnClickListener(...)' is the same as 'onClick(...)'. I just don't know how to close this question.

gihooh
  • 303
  • 1
  • 6
  • 16
  • learn some java basics ... do you even know what are the interfaces? you are not calling OnClick(this) but setOnClickListener which accepts OnClickListener interface ... – Selvin Jul 31 '14 at 12:39
  • This may also be useful...http://developer.android.com/guide/topics/ui/ui-events.html#EventListeners – codeMagic Jul 31 '14 at 12:43
  • I see now onClick is a method of setOnClickListener. Thanks @Selvin – gihooh Jul 31 '14 at 12:50
  • 1
    @gihooh No, it is not. onClick is not a method of setOnClickListener. onClick is a method from the onClickListener interface. setOnClickListener is a method in the Button class, which sets a listener that should be triggered when a click event occurs. I **strongly recommend you to learn the java basics**. – flotothemoon Jul 31 '14 at 12:55
  • I see I copy pasted the wrong word from the comment above. Thanks anyway for the advice @1337. – gihooh Jul 31 '14 at 13:05
  • @gihooh **The parameter has to be an instance of some object that implements the OnClickListener interface** see [below answer](http://stackoverflow.com/a/25059146/991085) and also http://stackoverflow.com/questions/1972579/android-how-to-set-a-named-method-in-button-setonclicklistener – Giru Bhai Jul 31 '14 at 13:15

3 Answers3

2

Look,

Understand This Two Lines First

View.OnClickListener - Interface definition for a callback to be invoked when a view is clicked.
onClick() - Called when a view has been clicked.

Now,
Your Activity is Implementing OnClickListener

   public class MainActivity extends Activity implements OnClickListener{}

So When you implement that... there is a method automatically implemented to class called onClick()

So when you pass this keyword to onClickListener of Any view. it means you are actually calling onClick() Method..

btn1.setOnClickListener(this); //calling onClick() method for button1
btn2.setOnClickListener(this); //calling onClick() method for button2

in onClick() method,
you just have to find your ID and write code for what you want to do with the view..

public void onClick(View v){
int id=v.get(); // getting ID of Clicked View

switch(id){

case R.id.button1:
    //called when you press button 1
    //write your code 
    break;
case R.id.button2:
    //called when you press button 2
    //write your code 
    break;
...
}

Please visit this website/blog for more detail about onClickListener() https://stand2code.blogspot.in/2016/12/multiple-buttons-onclicklistener-android.html

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
  • Thank you brother for the effort very well said. But I just overlooked the code thinking that `OnClickListener` interface == `setOnClickListener(...)` == `onClick(...)`. Anyway is there a way to remove this post because I think it is misleading. – gihooh Jul 31 '14 at 13:21
0

You are extending Activity but you are implementing OnClickListener too,and onClick() is a method of OnClickListenerinterface.Hence you have to implement its onClick method in your class and thus this is not raising any conflict.See this to learn about interfaces Is there more to an interface than having the correct methods

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72
0

setOnClickListener take an OnClickListener instance as parameter and OnClickListener is an interface which content an onClick() method.

The parameter has to be an instance of some object that implements the OnClickListener interface as you are passing here setOnClickListener(this); current context.

For more info see : Android - How to set a named method in button.setOnClickListener()

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74