12

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

Hunt
  • 8,215
  • 28
  • 116
  • 256

3 Answers3

30

Use

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

then use addView(myView)

AlbeyAmakiir
  • 2,217
  • 6
  • 25
  • 48
Ramya K Sharma
  • 743
  • 8
  • 16
9

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.

Ryan M
  • 18,333
  • 31
  • 67
  • 74
Raja Jawahar
  • 6,742
  • 9
  • 45
  • 56
0

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.

darune
  • 959
  • 8
  • 13