Android and getting a cast id view as a string

In the Java code of an Android project, if you want a link for a view resource, you can do something like:

View addButton = findViewById(R.id.button_0); 

In the above R.id.button_0 is not a String. Is it possible to dynamically refer to a resource using a string, for example, " R.id.button_0 "?

I would like to refer to the button on " R.id.button_%i ", where %i is replaced with some valid index.

+24
java android layout
Jan 18 2018-11-18T00:
source share
2 answers
 int resID = getResources().getIdentifier("button_%i", "id", getPackageName()); View addButton = findViewById(resID); 

where %i is replaced by some valid index.

The getResources() method belongs to the Context class, so you can use it directly from Activity . If you are not in action, use the context for access: ( myCtxt.getResources() ).

+52
Jan 18 '11 at 23:13
source share

You can try to place all the identifiers you want in the array, and then use this array to dynamically access your resources.

+4
Jan 18 '11 at 23:14
source share



All Articles