Create views programmatically using XML on Android

I believe you want the LayoutInflater. Let’s say your example XML is in a file custom_layout.xml:

LayoutInflater inflater = LayoutInflater.from(context);
RelativeLayout layout = (RelativeLayout) inflater.inflate(R.layout.custom_layout, null, false);

LinearLayout linear = (LinearLayout)findViewById(R.id.myLayout);
linear.addView(layout);

Leave a Comment

tech