Android View layoutInflater get Layout by string

  android, java, layout, string

having problems on android to get View with layoutInflater by using dynamically string as id.

This code works:

LayoutInflater layoutInflater = (LayoutInflater) 
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newView = layoutInflater.inflate(R.layout.my_layout, null);
scrollViewMain.removeAllViews();
scrollViewMain.addView(newView);

But I want to get the view dynamically by using a string like this:

String myString = "my_layout";
LayoutInflater layoutInflater = (LayoutInflater) 
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View newView = layoutInflater.inflate(R.layout.myString, null);
scrollViewMain.removeAllViews();
scrollViewMain.addView(newView);

Thanks for help understand this.

Source: Android Questions

LEAVE A COMMENT