I have a block of Views containing an ImageView, a Button and a TextView. I want to create this block dynamically in the Java code. Is there a way to define this block in XML, alter the src/text attributes and append it to my current layout?
Thanks!
Ron
I use the following technique:
Then inflate using a
LayoutInflater:I found this especially useful when you write a custom
Viewextending aViewGroup(egLinearLayoutorRelativeLayout) and you want to define the content in a declarative XML. For exampleA possible variation is using a
Layoutinstead of<merge>if you don’t need it. TheLayoutInflatercan be obtained byActivity.getLayoutInflater()or by anyContextviagetSystemService()(you have to cast the result to aLayoutInflaterobject in the latter case.