Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to create an app which plays a song with the name same as the button name.

then I came across this answer which used the command in the question.

I want to know what the command actually does.

Please explain in simple words.

What getResources().getIdentifier does, is to return the specific resource numeric identifier given its actual resource name. Check the documentation for more specifics.

Documentation quote:

Return a resource identifier for the given resource name.

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

A more clear explanation:

Lets say we must use a resource in code, such as for example:

view.setBackground(R.drawable.my_red_box)

In this example my_red_box is the name of the resource, but this name is actually an integer constant located in the resources 'R' file, which is autogenerated by gradle. So effectively, my_red_box is an integer constant, which Android uses to identify a specific resource file.

You can check a resource integer value by pressing the Ctrl key in your keyboard and hover with the mouse over the name of the resource in your code. If you are more curious about the resource constants, then you can locate them in app/build/intermediates/runtime_symbol_list//R.txt

So, to clarify, whenever you type any resource identifier (R.drawable, R.raw, etc), you are actually typing an integer constant, and the system uses this constant to reference a resource file.

What getResources().getIdentifier does is to allow resolving resource integer constants using the name of the resource.

For example if we want to set the background in a View, we can do as usual:

// Set the resource by using the its identifier (resource integer constant)
view.setBackground(R.drawable.my_red_box)

But with getResources().getIdentifier will be as next:

int resourceID = getResources().getIdentifier("my_red_box", "drawable", getPackageName());
view.setBackground(resourceID);

In this last example the value returned in resourceID is exactly the same one as R.drawable.my_red_box.

The purpose of getResources().getIdentifier, is to obtain resources dynamically. The are many use cases, for example if you have several resources with the same name ending with a numeric index, then you could make a for loop and autogenerate the name dynamically, so "my_red_box_1", "my_red_box_2", etc.

for (int index = 0; index < 3; index++) {
    int resourceID = getResources().getIdentifier("my_red_box_" + index, "drawable", getPackageName());

WARNING:

Using getResources().getIdentifier is not recommended, because Android can't know at compile time if the resource is being used, and therefore can be potentially removed when obfuscating / minimizing your code in a release build. In such case you need to add a proguard rule to keep the specific resources to avoid their removal.

What do you mean by Return a resource identifier for the given resource name.? Can you tell that in simple words? I couldn't understand the documentation. – Aditya Oct 2, 2020 at 5:15

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.