views:

29

answers:

1

I have a custom class called NoteView that extends FrameLayout. Previously, I had just been using the stock LinearLayout and have an XML layout built already that I want to reuse by adding that entire hierarchy (with some other overlay views which is why I needed framelayout) to the NoteView.

The problem is I can't figure out how to inflate the XML and add it to the NoteView from within the NoteView Class. I can add it after initialization is complete, but I want to be able to be able to inflate that hierarchy and add it automatically when my NoteView is either instantiated or inflated from XML.

How can I populate my extended FrameLayout with a layout from XML by adding it from within the NoteView class itself?

+1  A: 

Try LayoutInflator

LayoutInflater inflater = (LayoutInflater)context.getSystemService
  Context.LAYOUT_INFLATER_SERVICE);

LinearLayout myView = (LinarLayout)inflator.inflate(R.layout.my_layout, null);

frameLayout.addChild(myView);

Have a look at the documentation for LayoutInflator here: http://d.android.com/reference/android/view/LayoutInflater.html

disretrospect
I tried that but it didn't work at first. I just realized though that I was casting my top level view to the wrong class which was causing it to crash. This whole question is a red herring. :)
CodeFusionMobile