How to find View from string instead of R.id

You can use something like this:

Resources res = getResources();
int id = res.getIdentifier("titleText", "id", getContext().getPackageName());

And then use the id.


In Kotlin you can use this line to get the ID.

val id: Int = resources.getIdentifier("titleText", "id", packageName)

The returned value can be use as parameter of the function findViewById<TextView>(id).

NOTE: If is called outside of an activity use the context.

val id: Int = context.resources.getIdentifier("titleText", "id", context.packageName)