What is the meaning of a dollar sign before an Android resource id

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
34
down vote

favorite
3












In the accepted answer of the following post(Android custom numeric keyboard) I found a syntax that I don't understand:



$(R.id.t9_key_0).setOnClickListener(this);


What does the dollar sign mean in front? Is it specifically related to Android resource ids or is more a general Java syntax? Search engine results didn't show any suitable results.







share|improve this question




















  • Are they trying to make jQuery in Java? o.O
    – NoOneIsHere
    Aug 9 at 21:17






  • 1




    jQuery is good. you should definitely try jQuery.
    – Sp0T
    Aug 10 at 4:50














up vote
34
down vote

favorite
3












In the accepted answer of the following post(Android custom numeric keyboard) I found a syntax that I don't understand:



$(R.id.t9_key_0).setOnClickListener(this);


What does the dollar sign mean in front? Is it specifically related to Android resource ids or is more a general Java syntax? Search engine results didn't show any suitable results.







share|improve this question




















  • Are they trying to make jQuery in Java? o.O
    – NoOneIsHere
    Aug 9 at 21:17






  • 1




    jQuery is good. you should definitely try jQuery.
    – Sp0T
    Aug 10 at 4:50












up vote
34
down vote

favorite
3









up vote
34
down vote

favorite
3






3





In the accepted answer of the following post(Android custom numeric keyboard) I found a syntax that I don't understand:



$(R.id.t9_key_0).setOnClickListener(this);


What does the dollar sign mean in front? Is it specifically related to Android resource ids or is more a general Java syntax? Search engine results didn't show any suitable results.







share|improve this question












In the accepted answer of the following post(Android custom numeric keyboard) I found a syntax that I don't understand:



$(R.id.t9_key_0).setOnClickListener(this);


What does the dollar sign mean in front? Is it specifically related to Android resource ids or is more a general Java syntax? Search engine results didn't show any suitable results.









share|improve this question











share|improve this question




share|improve this question










asked Aug 9 at 12:16









Greg Holst

17517




17517











  • Are they trying to make jQuery in Java? o.O
    – NoOneIsHere
    Aug 9 at 21:17






  • 1




    jQuery is good. you should definitely try jQuery.
    – Sp0T
    Aug 10 at 4:50
















  • Are they trying to make jQuery in Java? o.O
    – NoOneIsHere
    Aug 9 at 21:17






  • 1




    jQuery is good. you should definitely try jQuery.
    – Sp0T
    Aug 10 at 4:50















Are they trying to make jQuery in Java? o.O
– NoOneIsHere
Aug 9 at 21:17




Are they trying to make jQuery in Java? o.O
– NoOneIsHere
Aug 9 at 21:17




1




1




jQuery is good. you should definitely try jQuery.
– Sp0T
Aug 10 at 4:50




jQuery is good. you should definitely try jQuery.
– Sp0T
Aug 10 at 4:50












2 Answers
2






active

oldest

votes

















up vote
40
down vote



accepted










It's a method call where the method name is $. The method is defined as follows in the code you linked:



protected <T extends View> T $(@IdRes int id) 
return (T) super.findViewById(id);



The method is a helper that removes the need to cast the return type of findViewById(). It's no longer needed as of Android O as the platform findViewById() uses generics to do the same.



The name $ is likely inspired by jQuery.






share|improve this answer





























    up vote
    1
    down vote













    Earlier days we know we needed to cast every return type of findViewById() method. Like



    usual way



    TextView textView = (TextView) findViewById(R.id.textView);


    This guy way



    TextView textView = $(R.id.textView);


    He ignored typecasting by his generic method.



    So the guy used Java generic to ignore type casting all the findViewById();. If you don't understand Generics, please read Why to use generics.



    protected <T extends View> T $(@IdRes int id) 
    return (T) super.findViewById(id);



    So now he doesn't need to type cast



    TextView textView = $(R.id.textView);


    Explanation of this method.



    • He created a method which accept resource id. So he can pass an Id.

    • He annotated this parameter by @IdRes so that Android Studio only allow resource ids in this parameter.

    • Then he called super class method findViewById which returns View.

    • He returned <T extends View> from method, so you will always have View object in return type.

    Important



    Now you don't need to make your generic methods. Because Android itself has changed his method. See Android Oreo Changes for findViewById().




    All instances of the findViewById() method now return
    T instead of View.




    Now you also can do same like that guy without typecasting



    TextView textView = findViewById(R.id.textView);





    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',
      convertImagesToLinks: true,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      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%2f51766807%2fwhat-is-the-meaning-of-a-dollar-sign-before-an-android-resource-id%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      40
      down vote



      accepted










      It's a method call where the method name is $. The method is defined as follows in the code you linked:



      protected <T extends View> T $(@IdRes int id) 
      return (T) super.findViewById(id);



      The method is a helper that removes the need to cast the return type of findViewById(). It's no longer needed as of Android O as the platform findViewById() uses generics to do the same.



      The name $ is likely inspired by jQuery.






      share|improve this answer


























        up vote
        40
        down vote



        accepted










        It's a method call where the method name is $. The method is defined as follows in the code you linked:



        protected <T extends View> T $(@IdRes int id) 
        return (T) super.findViewById(id);



        The method is a helper that removes the need to cast the return type of findViewById(). It's no longer needed as of Android O as the platform findViewById() uses generics to do the same.



        The name $ is likely inspired by jQuery.






        share|improve this answer
























          up vote
          40
          down vote



          accepted







          up vote
          40
          down vote



          accepted






          It's a method call where the method name is $. The method is defined as follows in the code you linked:



          protected <T extends View> T $(@IdRes int id) 
          return (T) super.findViewById(id);



          The method is a helper that removes the need to cast the return type of findViewById(). It's no longer needed as of Android O as the platform findViewById() uses generics to do the same.



          The name $ is likely inspired by jQuery.






          share|improve this answer














          It's a method call where the method name is $. The method is defined as follows in the code you linked:



          protected <T extends View> T $(@IdRes int id) 
          return (T) super.findViewById(id);



          The method is a helper that removes the need to cast the return type of findViewById(). It's no longer needed as of Android O as the platform findViewById() uses generics to do the same.



          The name $ is likely inspired by jQuery.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 10 at 5:08

























          answered Aug 9 at 12:19









          laalto

          112k26179220




          112k26179220






















              up vote
              1
              down vote













              Earlier days we know we needed to cast every return type of findViewById() method. Like



              usual way



              TextView textView = (TextView) findViewById(R.id.textView);


              This guy way



              TextView textView = $(R.id.textView);


              He ignored typecasting by his generic method.



              So the guy used Java generic to ignore type casting all the findViewById();. If you don't understand Generics, please read Why to use generics.



              protected <T extends View> T $(@IdRes int id) 
              return (T) super.findViewById(id);



              So now he doesn't need to type cast



              TextView textView = $(R.id.textView);


              Explanation of this method.



              • He created a method which accept resource id. So he can pass an Id.

              • He annotated this parameter by @IdRes so that Android Studio only allow resource ids in this parameter.

              • Then he called super class method findViewById which returns View.

              • He returned <T extends View> from method, so you will always have View object in return type.

              Important



              Now you don't need to make your generic methods. Because Android itself has changed his method. See Android Oreo Changes for findViewById().




              All instances of the findViewById() method now return
              T instead of View.




              Now you also can do same like that guy without typecasting



              TextView textView = findViewById(R.id.textView);





              share|improve this answer
























                up vote
                1
                down vote













                Earlier days we know we needed to cast every return type of findViewById() method. Like



                usual way



                TextView textView = (TextView) findViewById(R.id.textView);


                This guy way



                TextView textView = $(R.id.textView);


                He ignored typecasting by his generic method.



                So the guy used Java generic to ignore type casting all the findViewById();. If you don't understand Generics, please read Why to use generics.



                protected <T extends View> T $(@IdRes int id) 
                return (T) super.findViewById(id);



                So now he doesn't need to type cast



                TextView textView = $(R.id.textView);


                Explanation of this method.



                • He created a method which accept resource id. So he can pass an Id.

                • He annotated this parameter by @IdRes so that Android Studio only allow resource ids in this parameter.

                • Then he called super class method findViewById which returns View.

                • He returned <T extends View> from method, so you will always have View object in return type.

                Important



                Now you don't need to make your generic methods. Because Android itself has changed his method. See Android Oreo Changes for findViewById().




                All instances of the findViewById() method now return
                T instead of View.




                Now you also can do same like that guy without typecasting



                TextView textView = findViewById(R.id.textView);





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Earlier days we know we needed to cast every return type of findViewById() method. Like



                  usual way



                  TextView textView = (TextView) findViewById(R.id.textView);


                  This guy way



                  TextView textView = $(R.id.textView);


                  He ignored typecasting by his generic method.



                  So the guy used Java generic to ignore type casting all the findViewById();. If you don't understand Generics, please read Why to use generics.



                  protected <T extends View> T $(@IdRes int id) 
                  return (T) super.findViewById(id);



                  So now he doesn't need to type cast



                  TextView textView = $(R.id.textView);


                  Explanation of this method.



                  • He created a method which accept resource id. So he can pass an Id.

                  • He annotated this parameter by @IdRes so that Android Studio only allow resource ids in this parameter.

                  • Then he called super class method findViewById which returns View.

                  • He returned <T extends View> from method, so you will always have View object in return type.

                  Important



                  Now you don't need to make your generic methods. Because Android itself has changed his method. See Android Oreo Changes for findViewById().




                  All instances of the findViewById() method now return
                  T instead of View.




                  Now you also can do same like that guy without typecasting



                  TextView textView = findViewById(R.id.textView);





                  share|improve this answer












                  Earlier days we know we needed to cast every return type of findViewById() method. Like



                  usual way



                  TextView textView = (TextView) findViewById(R.id.textView);


                  This guy way



                  TextView textView = $(R.id.textView);


                  He ignored typecasting by his generic method.



                  So the guy used Java generic to ignore type casting all the findViewById();. If you don't understand Generics, please read Why to use generics.



                  protected <T extends View> T $(@IdRes int id) 
                  return (T) super.findViewById(id);



                  So now he doesn't need to type cast



                  TextView textView = $(R.id.textView);


                  Explanation of this method.



                  • He created a method which accept resource id. So he can pass an Id.

                  • He annotated this parameter by @IdRes so that Android Studio only allow resource ids in this parameter.

                  • Then he called super class method findViewById which returns View.

                  • He returned <T extends View> from method, so you will always have View object in return type.

                  Important



                  Now you don't need to make your generic methods. Because Android itself has changed his method. See Android Oreo Changes for findViewById().




                  All instances of the findViewById() method now return
                  T instead of View.




                  Now you also can do same like that guy without typecasting



                  TextView textView = findViewById(R.id.textView);






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 24 at 5:47









                  Khemraj

                  6,07821140




                  6,07821140



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51766807%2fwhat-is-the-meaning-of-a-dollar-sign-before-an-android-resource-id%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      What does second last employer means? [closed]

                      One-line joke