4

I noticed that the Activity class has a setContentView method where an xml resource file can be loaded. I wanted to do the same thing with a class that inherits ultimately from View. This seemed to be a dead end because the setContentView method does not exist for the View class.

This leads to a couple of questions:

1) Is it possible for View's to load layouts created in the Visual Layout Editor?

2) If not, why? It seems like not allowing users to load an xml layout directly into a View is a limitation. I expect that there is a reason why the setContentView method (or a method similar) is not provided in the API.

Thanks in advance!

JRL
  • 83
  • 1
  • 7

1 Answers1

5

I think that LayoutInflator is what you are looking for http://developer.android.com/reference/android/view/LayoutInflater.html And here is a code sample:

    View view = (View)inflater.inflate(R.layout.abc, null);
    view_group.add(view);
gregory561
  • 14,866
  • 2
  • 23
  • 25
  • Worked like a charm. Thanks Greg! Wonder why this never popped up in any Google searches... – JRL Jul 04 '11 at 14:57
  • 1
    After trying out the solution, I also found this online: http://blog.jayway.com/2009/03/26/layout-resources-in-android/ Could be helpful for anyone else who is new to Android. – JRL Jul 04 '11 at 16:19