I would put some container view at the position you want your layout to be like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rellay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/placesgradient">
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="click"
/>
<FrameLayout
android:id="@+id/flContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@id/btn2"
android:text="hello this is text..see above" />
</RelativeLayout>
Then you can retrieve the container in your Fragment like this:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.home, container, false);
// Retrieve your container
flContainer = rootView.findViewById(R.id.flContainer);
b1 = (Button) rootView.findViewById(R.id.btn2);
b1.setOnClickListener(this);
return rootView;
}
And later when you want to add your layout you can add a child to the FrameLayout
like this:
flContainer.addView(subLayout);
If you later want to change the layout in your container you can do it like this:
flContainer.removeAllViews();
flContainer.addView(otherLayout);