How to use getResource.getIdentifier() to get Layout?

With this:

int layoutID = getResources().getIdentifier("layout"+n, "layout", getPackageName());

you basically retrieve the id of a layout file that you can inflate. It’s the dynamic version of

int layoutID = R.layout.layout1;

What you intend to do is retrieve a view from an already inflated layout. That’s how you’d do it:

int layoutID = getResources().getIdentifier("layout"+n, "id", getPackageName());
return (LinearLayout)findViewById(layoutID);

That’s the dynamic version of

return (LinearLayout)findViewById(R.id.layout1);

Leave a Comment