I have a layout defined in XML. It contains also:
<RelativeLayout
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
I would like to inflate this RelativeView with other XML layout file. I may use different layouts depending on a situation. How should I do it? I was trying different variations of
RelativeLayout item = (RelativeLayout) findViewById(R.id.item);
item.inflate(...)
But none of them worked fine.
AttachToRoot Set to True
Just think we specified a button in an XML layout file with its layout width and layout height set to match_parent.
On This Buttons Click Event We Can Set Following Code to Inflate Layout on This Activity.
Hope this solution works for you.!
I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
If you want to add a single view multiple time then you have to use
If you do like
and
then it will throw exception of all ready added view.
If you're not in an activity you can use the static
from()
method from theLayoutInflater
class to get aLayoutInflater
, or request the service from the context methodgetSystemService()
too :(I know it's almost 4 years ago but still worth mentioning)
If you are you trying to attach a child view to the RelativeLayout? you can do by following
layout inflation