btn.setOnClickListener() how does it calls





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I am beginner in programing, and I know how btn.setOnClickListener{} function works (curly brackets).
But there is other tipe of function btn.setOnClickListener() - brackets are not curly. I do not know how and when I should use this tipe of functions. How does such type of function calls? I would like to learn more about it but I do not know how to google it
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"










share|improve this question

























  • According to this article: antonioleiva.com/lambdas-kotlin-android paranthesis () are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"

    – Feelsbadman
    Jan 3 at 22:54


















-1















I am beginner in programing, and I know how btn.setOnClickListener{} function works (curly brackets).
But there is other tipe of function btn.setOnClickListener() - brackets are not curly. I do not know how and when I should use this tipe of functions. How does such type of function calls? I would like to learn more about it but I do not know how to google it
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"










share|improve this question

























  • According to this article: antonioleiva.com/lambdas-kotlin-android paranthesis () are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"

    – Feelsbadman
    Jan 3 at 22:54














-1












-1








-1








I am beginner in programing, and I know how btn.setOnClickListener{} function works (curly brackets).
But there is other tipe of function btn.setOnClickListener() - brackets are not curly. I do not know how and when I should use this tipe of functions. How does such type of function calls? I would like to learn more about it but I do not know how to google it
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"










share|improve this question
















I am beginner in programing, and I know how btn.setOnClickListener{} function works (curly brackets).
But there is other tipe of function btn.setOnClickListener() - brackets are not curly. I do not know how and when I should use this tipe of functions. How does such type of function calls? I would like to learn more about it but I do not know how to google it
Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"







kotlin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 5 at 21:27







Павел Малиновский

















asked Jan 3 at 22:50









Павел МалиновскийПавел Малиновский

913




913













  • According to this article: antonioleiva.com/lambdas-kotlin-android paranthesis () are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"

    – Feelsbadman
    Jan 3 at 22:54



















  • According to this article: antonioleiva.com/lambdas-kotlin-android paranthesis () are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"

    – Feelsbadman
    Jan 3 at 22:54

















According to this article: antonioleiva.com/lambdas-kotlin-android paranthesis () are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"

– Feelsbadman
Jan 3 at 22:54





According to this article: antonioleiva.com/lambdas-kotlin-android paranthesis () are removed when there's only 1 parameter and it's a function: "If a function has only one parameter, and this is a function, the parentheses can be deleted"

– Feelsbadman
Jan 3 at 22:54












4 Answers
4






active

oldest

votes


















0














According to : https://antonioleiva.com/lambdas-kotlin-android/



If a function has only one parameter, and this is a function, the parentheses can be deleted



Instead of having empty parentheses, we can better delete them:


view.setOnClickListener { v -> toast("Hello") }





If the function’s last parameter is a function, it can go outside the parentheses



Therefore, we can extract the listener as follows:


view.setOnClickListener() { v -> toast("Hello") }


If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.





share|improve this answer































    0














    Both types are equivalent:



    button.setOnClickListener {
    // ......................
    }

    button.setOnClickListener(View.OnClickListener {
    // ......................
    })


    but the 1st one is the preferred way to go.

    Even if you write the 2nd one, if you hover the mouse over View.OnClickListener,
    Android Studio will pop up this message:




    Redundant SAM constructor




    and if you press Alt-Enter you will be prompted:




    Remove redundant SAM constructor




    and if you click on it then View.OnClickListener will be removed.

    Again by pressing Alt-Enter you will be prompted:




    Move lambda argument out of parentheses




    and if you click on it then you will get the 1st type.

    So don't worry about it, use the 1st type and you will be fine.






    share|improve this answer































      0














      Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"






      share|improve this answer































        0














        setOnClickListener(Interface i)



         This is Method of View Class in Android.
        1. setOnClickListener is method of that class which except only interface as a parameter.

        2. or else you have to implement that interface in your class like given example.

        Go through Anonymous Class implement process.

        the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.




        1. When you want to implement in class use this

        btnView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
        });


        2. Pass interface object of onClickListener

        appView.setOnClickListener(); ;----> pass interface object





        share|improve this answer
























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030889%2fbtn-setonclicklistener-how-does-it-calls%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          According to : https://antonioleiva.com/lambdas-kotlin-android/



          If a function has only one parameter, and this is a function, the parentheses can be deleted



          Instead of having empty parentheses, we can better delete them:


          view.setOnClickListener { v -> toast("Hello") }





          If the function’s last parameter is a function, it can go outside the parentheses



          Therefore, we can extract the listener as follows:


          view.setOnClickListener() { v -> toast("Hello") }


          If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.





          share|improve this answer




























            0














            According to : https://antonioleiva.com/lambdas-kotlin-android/



            If a function has only one parameter, and this is a function, the parentheses can be deleted



            Instead of having empty parentheses, we can better delete them:


            view.setOnClickListener { v -> toast("Hello") }





            If the function’s last parameter is a function, it can go outside the parentheses



            Therefore, we can extract the listener as follows:


            view.setOnClickListener() { v -> toast("Hello") }


            If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.





            share|improve this answer


























              0












              0








              0







              According to : https://antonioleiva.com/lambdas-kotlin-android/



              If a function has only one parameter, and this is a function, the parentheses can be deleted



              Instead of having empty parentheses, we can better delete them:


              view.setOnClickListener { v -> toast("Hello") }





              If the function’s last parameter is a function, it can go outside the parentheses



              Therefore, we can extract the listener as follows:


              view.setOnClickListener() { v -> toast("Hello") }


              If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.





              share|improve this answer













              According to : https://antonioleiva.com/lambdas-kotlin-android/



              If a function has only one parameter, and this is a function, the parentheses can be deleted



              Instead of having empty parentheses, we can better delete them:


              view.setOnClickListener { v -> toast("Hello") }





              If the function’s last parameter is a function, it can go outside the parentheses



              Therefore, we can extract the listener as follows:


              view.setOnClickListener() { v -> toast("Hello") }


              If we had more parameters, the rest of the parameters would go inside the parentheses, even if these were functions. Only the last parameter can be extracted.






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Jan 3 at 23:00









              FeelsbadmanFeelsbadman

              696724




              696724

























                  0














                  Both types are equivalent:



                  button.setOnClickListener {
                  // ......................
                  }

                  button.setOnClickListener(View.OnClickListener {
                  // ......................
                  })


                  but the 1st one is the preferred way to go.

                  Even if you write the 2nd one, if you hover the mouse over View.OnClickListener,
                  Android Studio will pop up this message:




                  Redundant SAM constructor




                  and if you press Alt-Enter you will be prompted:




                  Remove redundant SAM constructor




                  and if you click on it then View.OnClickListener will be removed.

                  Again by pressing Alt-Enter you will be prompted:




                  Move lambda argument out of parentheses




                  and if you click on it then you will get the 1st type.

                  So don't worry about it, use the 1st type and you will be fine.






                  share|improve this answer




























                    0














                    Both types are equivalent:



                    button.setOnClickListener {
                    // ......................
                    }

                    button.setOnClickListener(View.OnClickListener {
                    // ......................
                    })


                    but the 1st one is the preferred way to go.

                    Even if you write the 2nd one, if you hover the mouse over View.OnClickListener,
                    Android Studio will pop up this message:




                    Redundant SAM constructor




                    and if you press Alt-Enter you will be prompted:




                    Remove redundant SAM constructor




                    and if you click on it then View.OnClickListener will be removed.

                    Again by pressing Alt-Enter you will be prompted:




                    Move lambda argument out of parentheses




                    and if you click on it then you will get the 1st type.

                    So don't worry about it, use the 1st type and you will be fine.






                    share|improve this answer


























                      0












                      0








                      0







                      Both types are equivalent:



                      button.setOnClickListener {
                      // ......................
                      }

                      button.setOnClickListener(View.OnClickListener {
                      // ......................
                      })


                      but the 1st one is the preferred way to go.

                      Even if you write the 2nd one, if you hover the mouse over View.OnClickListener,
                      Android Studio will pop up this message:




                      Redundant SAM constructor




                      and if you press Alt-Enter you will be prompted:




                      Remove redundant SAM constructor




                      and if you click on it then View.OnClickListener will be removed.

                      Again by pressing Alt-Enter you will be prompted:




                      Move lambda argument out of parentheses




                      and if you click on it then you will get the 1st type.

                      So don't worry about it, use the 1st type and you will be fine.






                      share|improve this answer













                      Both types are equivalent:



                      button.setOnClickListener {
                      // ......................
                      }

                      button.setOnClickListener(View.OnClickListener {
                      // ......................
                      })


                      but the 1st one is the preferred way to go.

                      Even if you write the 2nd one, if you hover the mouse over View.OnClickListener,
                      Android Studio will pop up this message:




                      Redundant SAM constructor




                      and if you press Alt-Enter you will be prompted:




                      Remove redundant SAM constructor




                      and if you click on it then View.OnClickListener will be removed.

                      Again by pressing Alt-Enter you will be prompted:




                      Move lambda argument out of parentheses




                      and if you click on it then you will get the 1st type.

                      So don't worry about it, use the 1st type and you will be fine.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 3 at 23:07









                      forpasforpas

                      19.9k4830




                      19.9k4830























                          0














                          Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"






                          share|improve this answer




























                            0














                            Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"






                            share|improve this answer


























                              0












                              0








                              0







                              Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"






                              share|improve this answer













                              Answer: "If a function has only one parameter, and this is a function, the parentheses can be deleted"







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Feb 7 at 13:37









                              Павел МалиновскийПавел Малиновский

                              913




                              913























                                  0














                                  setOnClickListener(Interface i)



                                   This is Method of View Class in Android.
                                  1. setOnClickListener is method of that class which except only interface as a parameter.

                                  2. or else you have to implement that interface in your class like given example.

                                  Go through Anonymous Class implement process.

                                  the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.




                                  1. When you want to implement in class use this

                                  btnView.setOnClickListener(new View.OnClickListener() {
                                  @Override
                                  public void onClick(View v) {

                                  }
                                  });


                                  2. Pass interface object of onClickListener

                                  appView.setOnClickListener(); ;----> pass interface object





                                  share|improve this answer




























                                    0














                                    setOnClickListener(Interface i)



                                     This is Method of View Class in Android.
                                    1. setOnClickListener is method of that class which except only interface as a parameter.

                                    2. or else you have to implement that interface in your class like given example.

                                    Go through Anonymous Class implement process.

                                    the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.




                                    1. When you want to implement in class use this

                                    btnView.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                    }
                                    });


                                    2. Pass interface object of onClickListener

                                    appView.setOnClickListener(); ;----> pass interface object





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      setOnClickListener(Interface i)



                                       This is Method of View Class in Android.
                                      1. setOnClickListener is method of that class which except only interface as a parameter.

                                      2. or else you have to implement that interface in your class like given example.

                                      Go through Anonymous Class implement process.

                                      the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.




                                      1. When you want to implement in class use this

                                      btnView.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {

                                      }
                                      });


                                      2. Pass interface object of onClickListener

                                      appView.setOnClickListener(); ;----> pass interface object





                                      share|improve this answer













                                      setOnClickListener(Interface i)



                                       This is Method of View Class in Android.
                                      1. setOnClickListener is method of that class which except only interface as a parameter.

                                      2. or else you have to implement that interface in your class like given example.

                                      Go through Anonymous Class implement process.

                                      the thing is that. either you pass interface object or you have to implement onClick method interface OnClickListener.




                                      1. When you want to implement in class use this

                                      btnView.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View v) {

                                      }
                                      });


                                      2. Pass interface object of onClickListener

                                      appView.setOnClickListener(); ;----> pass interface object






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 7 at 14:10









                                      ZafarHussainZafarHussain

                                      15727




                                      15727






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030889%2fbtn-setonclicklistener-how-does-it-calls%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Monofisismo

                                          Angular Downloading a file using contenturl with Basic Authentication

                                          Olmecas