Pievis
Pievis

Reputation: 1994

is it possible to instantiate a layout based on it's name?

I would like to create a framework that "builds" part of the application structure and presentation views at runtime, parsing some configuration file. What I'm wondering is if there's a way in with I can reference a layout and instantiate it based on a name without creating a map. for example instead out using simply:

ViewGroup layout = (ViewGroup) findViewWithId(R.id.layout1);

Doing something more like:

String name = Config.getLayout1();  
ViewGroup layout = getLayoutByName(name);

Upvotes: 1

Views: 46

Answers (1)

Sharjeel
Sharjeel

Reputation: 15798

When you get layout by R.id.layout1 it actually first gets it's resource ID and pass it to findViewWithID.

So you can find that ID yourself and send that to findViewWithId function.

You can do something like this:

String layoutName = "layout1";
int resID = context.getResources().getIdentifier(layoutName, "layout", context.getPackageName());

Then get the actual view:

ViewGroup layout = (ViewGroup) findViewWithId(resID);

Upvotes: 1

Related Questions