user1646615
user1646615

Reputation:

Add XML content to a View

im developing an application and I need to add elements dynamically. I wonder if I can append elements (stored in a XML file) in my current Activity, like innerHTML in JavaScript.

I tried LayoutInflater but that replaces all the content and I need to append.

Thanks!

Upvotes: 1

Views: 78

Answers (1)

Dan Harms
Dan Harms

Reputation: 4840

The easiest way to do this is to use the LayoutInflater as you said. I'm not sure how you were doing it (hence why I asked to see your inflating code), but the simplest way to understand is to do the following:

LayoutInflater inflater = getLayoutInflater();
View viewToAppend = inflater.inflate(R.layout.some_layout, null);
// Optional, create LayoutParams and apply to view with 
// viewToAppend.setLayoutParams(params);
mainView.addView(viewToAppend);

Upvotes: 1

Related Questions