Inflate Layout Programmatically Within Another Layout

Inflate layout programmatically within another layout

You could use something like

LayoutInflater inflater = LayoutInflater.from(context);

//to get the MainLayout
View view = inflater.inflate(container_destacado, null);
...
//Avoid pass null in the root it ignores spaces in the child layout

View inflatedLayout= inflater.inflate(R.layout.yourLayout, (ViewGroup) view, false);
containerDestacado.addView(inflatedLayout);

Inflate a view / layout into another layout?

There is ViewStub but I never used it and I think it can't be used more than once.

You can inflate the menu layout and attach it to the main layout:

AbsoluteLayout mainLayout = (AbsoluteLayout) findViewById(R.id.your_main_layout);
LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);

then when you want to change you can remove it:

mainLayout.removeView(menuLayout);

and add another the same way.

This will work because you want to add the layout as the last child of the parent layout. If you want to add it, say, at 1st position, you can inflate your layout without attaching it to the parent (use false as last arg), and adding it manually specifying the index:

mainLayout.addView(menuLayout, 0);

Programmatically Adding views in inflated layout

Your LinearLayout needs to have height greater than 0 to add views at run-time otherwise it won't be able to calculate the height.

For example make it wrap_content

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="SUBSCRIPTIONS"
android:textColor="#F5D90A"
android:textSize="23sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<LinearLayout
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

</androidx.constraintlayout.widget.ConstraintLayout>

add Layout programmatically inside another one

The inflate(int resource, ViewGroup root) method return root if root is not null, thus to_add.findViewById() equals options_layout.findViewById() and it always return the Views in the first position.
Change as following should help:

LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
View to_add = inflater.inflate(R.layout.options_element,
options_layout);

TextView text = (TextView) to_add.findViewById(R.id.text);
text.setText(options[i]);
text.setTypeface(FontSelector.getBold(getActivity()));

}

to:

LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
View to_add = inflater.inflate(R.layout.options_element,
options_layout,false);

TextView text = (TextView) to_add.findViewById(R.id.text);
text.setText(options[i]);
text.setTypeface(FontSelector.getBold(getActivity()));
options_layout.addView(to_add);
}

Inflate programmatically for android layout

Add a LinearLayout to your layout, and then inflate the header_description layout in code multiple times and add it to the LinearLayout.

Change the layout to be similar to this:

<include layout="@layout/header_list_image"/>

<LinearLayout
android:layout_id="@+id/description_layout"
android:layout_width="match_parent"
android:layout_height="wrap_contents"
android:orientation="vertical" >

<include layout="@layout/header_url"/>
<include layout="@layout/row_listing_like_comment_share"/>
<include layout="@layout/header_comment_separator"/>

In code, find the LinearLayout using the id, and the inflate the description layouts one at a time and add them in your onCreate() function:

LinearLayout layout = (LinearLayout)findViewById(R.id.description_layout);
LayoutInflater inflater = getLayoutInflater();

// add description layouts:
for(int i = 0; i < count; i++)
{
// inflate the header description layout and add it to the linear layout:
View descriptionLayout = inflater.inflate(R.layout.header_description, null, false);
layout.addView(descriptionLayout);

// you can set text here too:
TextView textView = descriptionLayout.findViewById(R.id.text_view);
textView.setText("some text");
}

Also, another problem is that if I reuse like that, "id" will be same. If so, how can I set text?

call findViewById() on the inflated layout e.g:

descriptionLayout.findViewById(R.id.text_view);

not like this:

findViewById(R.id.text_view);

Can we modify a layout (programmatically) before inflating it in a fragment?

Theoretically:

View view = inflater.inflate(R.layout.fragment_popup, container, false);

view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

return view;

Practically:

Never tried.



Related Topics



Leave a reply



Submit