Droidman
Droidman

Reputation: 11608

add an existing XML layout programatically

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..

Upvotes: 1

Views: 952

Answers (3)

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

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.

Upvotes: 4

Guna
Guna

Reputation: 121

Use LayoutInflater class for that. It will covert your layout into a view and add it to parent.

Upvotes: 0

Nermeen
Nermeen

Reputation: 15973

Yes, using LayoutInflater you will inflate your XML then add it to the parent..

check, What does LayoutInflater in Android do?

Upvotes: 1

Related Questions