How to inflate FrameLayout?
11,020
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
Related videos on Youtube
Author by
Waypoint
Updated on June 04, 2022Comments
-
Waypoint about 2 years
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);
-
kaspermoerch over 12 yearsPost some code of what you've tried so far.
-
-
Waypoint over 12 yearsLayoutInflater.fromContext(context) -> what does it exactly do? If I pass this, it shows error
-
Pal Szasz over 12 yearsLayoutInflater.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?
-
dbm over 12 years@Pal Szasz: I think you mean
LayoutInflater.from(context);