I'm going to create a new RelativeLayout with some Text and Image Views programatically and then add it to a parent layout. The question: is it possible to define that layout in XML and then just add it to the parent programatically? Would be a faster way..
3 Answers
Use inflation:
final LayoutInflater lyInflaterForPanel = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout lLayoutPanel = (LinearLayout) lyInflaterForPanel.inflate(
R.layout.my_row, null);
This has worked for me.
3 Comments
Droidman
thx, it works. BUT, the layout that I did inflate has some TextViews. I initialized them after inflating the layout and tried to set the text, but it causes crashes. Any ideas how to fix that?
MysticMagicϡ
I think it should work. Can you post logcat with your updated code?
Droidman
it gives me a "Delivery Failure" error since I tried so set the text in the OnActivityResult() method (caused by NullPointerException). I did initialize the TextView after inflating its parent layout, but any action other then initialization (findViewById) causes a NullPointerException..