Here is a suggestion: you can get the ID
(specified e.g. by android:id="@+id/..My Str..
) which was generated by R
by using its given name (e.g. My Str
). A code snippet using getIdentifier()
method would then be:
public int getIdAssignedByR(Context pContext, String pIdString)
{
// Get the Context's Resources and Package Name
Resources resources = pContext.getResources();
String packageName = pContext.getPackageName();
// Determine the result and return it
int result = resources.getIdentifier(pIdString, "id", packageName);
return result;
}
From within an Activity
, an example usage coupled with findViewById
would be:
// Get the View (e.g. a TextView) which has the Layout ID of "UserInput"
int rID = getIdAssignedByR(this, "UserInput")
TextView userTextView = (TextView) findViewById(rID);