Reputation: 1428
I have a fragment layout to which i want to add another sub layout dynamically ,
I tried this https://stackoverflow.com/a/22592269/8476022
sub-layout xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="Question"
android:textColor="@color/colorTextBlack"
android:textSize="16dp" />
</android.support.constraint.ConstraintLayout>
this is my main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f2f2f2"
android:orientation="vertical">
<FrameLayout
android:id="@+id/flContainer"
android:layout_width="match_parent"
android:layout_margin="20dp"
android:layout_height="wrap_content"/>
</LinearLayout>
inside my fragment i included the sublayout
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_start_question, container, false);
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
flContainer.addView(R.layout.sub_layout);
return v;
}
but i am getting error near addView the screenshot is below
can anyone please help to find the error
Upvotes: 2
Views: 3786
Reputation: 69689
You are getting Error because
addView(View child)
need parameter a view
and you are adding FrameLayout
which is ViewGroup
Try this way
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = inflater.inflate(R.layout.sub_layout, null);;
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
flContainer.addView(myView);
Upvotes: 9
Reputation: 1005
Replace your onCreateView with the following,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_start_question, container, false);
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
View subView = inflater.inflate(R.layout.sub_layout, null);
flContainer.addView(subView);
return v;
}
Upvotes: 1
Reputation: 199
You want to use:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_start_question, container, false);
// flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
// flContainer.addView(R.layout.sub_layout);
LinearLayout linPhoneNumber = (LinearLayout) v.findViewById(R.id.linPhoneNumber);
View viewPhoneNumber = getActivity().getLayoutInflater().inflate(R.layout.fragment_contact_phone, null);
LinearLayout linPhoneLayout = (LinearLayout) viewPhoneNumber.findViewById(R.id.linPhoneLayout);
TextView txtPhoneNumber = (TextView) viewPhoneNumber.findViewById(R.id.txtPhoneNumber);
txtPhoneNumber.setText(phone);
linPhoneNumber.addView(viewPhoneNumber);
return v;
}
put one linear layout in fragment_start_question.xml :
<LinearLayout
android:id="@+id/linPhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"></LinearLayout>
then fragment_contact_phone.xml
<LinearLayout
android:id="@+id/linPhoneLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_top_10"
android:orientation="horizontal"
android:padding="@dimen/margin_top">
<ImageView
android:id="@+id/imgCall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
android:src="@drawable/icon_call" />
<Textview
android:id="@+id/txtPhoneNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="@dimen/margin_top_1"
android:textColor="@color/menu_text_color"
android:textSize="@dimen/margin_top_1"/>
</LinearLayout>
Upvotes: 1
Reputation: 911
You are passing the layout id only,Define a layout first like:
LinearLayout sublayout = (LinearLayout)v.findViewBYId(R.layout.sub_layout)
flContainer.addView(sublayout);
Upvotes: 0
Reputation: 21043
Add view takes View object. You are passing the layout Id.
addView(View view)
you do not pass the layout Id
flContainer.addView(R.layout.sub_layout);// Wrong
First inflate a view using inflater then add it to container.
View view=LayoutInflater.from(getContext()).inflate(R.layout.yourView,null);
yourContainer.addView(view);
Upvotes: 2
Reputation: 1227
You need to inflate layout to add the view refer this
View child = getLayoutInflater().inflate(R.layout.sub_layout, null);
flContainer = (FrameLayout) v.findViewById(R.id.flContainer);
flContainer.addView(child);
Upvotes: 1