Hunt
Hunt

Reputation: 8425

add layout as a view in Android

I have a pretty complex layout defined in xml file , now I want to add this layout as a view using addView or something else.

As layout is pretty much complex , its pretty cumbersome process to write the code for layout and then add it as a view. So is there anyway to load layout resource and add it as a view.

I want to add the layout into WebView

Upvotes: 12

Views: 23127

Answers (3)

Raja Jawahar
Raja Jawahar

Reputation: 6952

You can also reduce this to one line of code;

  View view = View.inflate(getActivity, R.layout.my_layout,null);

then add to your view.

Upvotes: 9

Ramya K Sharma
Ramya K Sharma

Reputation: 743

Use

LayoutInflater factory = LayoutInflater.from(this);
View myView = factory.inflate(R.layout.my_layout_id, null);

then use addView(myView)

Upvotes: 30

darune
darune

Reputation: 959

If your view is a ViewGroup (layout) You can use InflaterService.inflate(id, ViewGroup) and set the ViewGroup, it will set the current child(s) with the content of your xml.

Upvotes: 0

Related Questions