0

I have the following xml:

fragment1.xml

<include
    android:id="@+id/customized_layout"
    android:layout_width="match_parent"
    android:layout_height="40dp"
   android:layout_gravity="bottom"
 />
..
/>

In this Fragment how can I add programmatically the layout I want to include in the customized_layout?

I have the following:

@InjectView (R.id. customized_layout) RelativeLayout customLayout;
@Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ButterKnife.inject(this, view);

      //customLayou.addLayout(...) ??
    }
1
  • why there is a -1 vote? I think this is a valid question. Commented Dec 19, 2017 at 9:36

2 Answers 2

2

Use LayoutInflater to inflate xml. And add view to a container.

LinearLayout container=(LinearLayout)findViewById(R.id.container);
View view= LayoutInflater.from(getActivity()).inflate(R.layout.item_view,null);
container.addView(view);
Sign up to request clarification or add additional context in comments.

2 Comments

I get the error message: You must specify a layout in the include tag: <include layout="@layout/layoutID" />
You do not have to use include tag when inflating dynamically just use a LinearLayout and it will work .
0

Suppose your dynamic layout is :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/dynamic_layout"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
     .........
</LinearLayout>

In your Activity:

LinearLayout dynamicLayout = findViewById(R.id.dynamic_layout)
customLayout.addView(dynamicLayout)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.