2

I have linear Layout and I want to inflate FrameLayout into it. Do you know, how it can be done? Is it possible? I am still getting errors of No Suitable Method Found For Inflate

Thanks

Edit: answering myself:

LinearLayout ll=(LinearLayout) findViewById(R.id.linear);
final LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout frml = (FrameLayout)inflater.inflate(R.layout.frame,null);
frml.setId(10101010);
frml.setBackgroundColor(Color.RED);

ll.addView(frml);
Waypoint
  • 17,283
  • 39
  • 116
  • 170

2 Answers2

2

To get the LayoutInflater instance you need to get it as a service like this

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Then you can use it to inflate the FrameLayout and add it to the LinearLayout like this

LinearLayout linearLayout = ... ;
inflater.inflate(R.layout.your_framelayout_file_name, linearLayout, false);

And don't forget to put the layout width and height for your FrameLayout like this

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    .
    .
    .
</FrameLayout>

For more infromation about the LayoutInflater visit this link

fadisdh
  • 552
  • 1
  • 4
  • 9
0
LinearLayout parent = ...
LayoutInflater li = LayoutInflater.from(context);
View view = li.inflate(R.layout.my_layout, parent, false);
parent.addView(view);
Pal Szasz
  • 2,954
  • 3
  • 20
  • 18
  • LayoutInflater.fromContext(context) -> what does it exactly do? If I pass this, it shows error – Waypoint Oct 27 '11 at 09:54
  • LayoutInflater.fromContext is more or less the same as getting a LayoutInflater instance from the service manager. I prefer this, since it makes more sense (LayoutInflater is not a service), and it doesn't contain a typecast. What error does it give you? – Pal Szasz Oct 27 '11 at 22:00
  • 1
    @Pal Szasz: I think you mean `LayoutInflater.from(context);` – dbm Nov 08 '11 at 13:53