Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

android

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.

like image 945
Greg Holst Avatar asked Aug 09 '18 12:08

Greg Holst


People also ask

What does dollar sign mean in Android?

It's a method call where the method name is $ .

What is resource id in Android?

For each type of resource, there is an R subclass (for example, R. drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R. drawable. icon ). This integer is the resource ID that you can use to retrieve your resource.

How do I put the dollar sign on my Android?

123 key in the lower-left corner of the keyboard. In the second row of numbers and symbols, press and hold your finger on the dollar-sign key. Above your finger, a box will pop up showing several currency symbols; these include the peso, the euro, the cent sign, the pound sterling and the yen.

What is the use of getResources() in Android?

The Android resource system keeps track of all non-code assets associated with an application. You can use this class to access your application's resources. You can generally acquire the Resources instance associated with your application with getResources() .


2 Answers

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.

like image 141
laalto Avatar answered Oct 11 '22 07:10

laalto


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); 
like image 38
Khemraj Sharma Avatar answered Oct 11 '22 05:10

Khemraj Sharma