Reputation: 1663
I want to inflate a layout multiple times using a loop into another layout. the java is fine when i inflate a TextView or any other single view. but i want to inflate this entire layout. What method do i use for this because the following code wont work. (like i said, the loop and all is fine, its just i cant inflate an entire layout. I only know how to inflate a simple view).
View childInflatedView = (View) theInflater.inflate(R.id.restaurant_displayed_on_list, null);
linLayout.addView(childInflatedView);
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dp"
android:orientation="horizontal"
android:background="@drawable/gradient_bg_hover"
android:id="@+id/restaurant_displayed_on_list" >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="6"
android:layout_height="fill_parent"
android:text="this is the inflated text"
android:textColor="#990099"
android:textSize="20dp"
android:gravity="center_horizontal"
/>
<Button
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:background= "@drawable/delete"
/>
</LinearLayout>
Edit1:
As stated by Matthew, I think the solution is to inflate the layout by layout resource ID. The problem I think lies in this line of code not shown above since the layout cannot be cast to a TextView:
View categoryInflatedView1 = (View) theInflater.inflate(R.layout.categories_available, null);
((TextView) categoryInflatedView1).setText(category1);
linLayout.addView(categoryInflatedView1);
How do i access the TextView that lies inside of the layout. I need to edit the textview before inflating.
Upvotes: 2
Views: 2694
Reputation: 1663
So the reason that this wasn't working was 2 fold.
You simply inflate the layout via layout resource ID as stated by Mathew
View childInflatedView = (View) theInflater.inflate(R.layout.restaurant_available, null);
I was having an extra error that i found in the log due to casting the layout to a TextView Instead, i must use this code to make it work (layout variable then find view by id):
((TextView) childInflatedView.findViewById(R.id.restaurant_displayed_on_list)).setText(currentlyDelRest1);
Although this answer is correct, I am going to assign the accepted answer to Mathew since his answer was correct given the information available in the question. Thanks for the help
Upvotes: 1
Reputation: 14038
Use the <include>
tag. It includes another XML layout within an existing one.For ex, adding the below two lines will include two other layouts in your parent layout:
<include android:id="@+id/cell1" layout="@layout/workspace_screen" />
<include android:id="@+id/cell2" layout="@layout/workspace_screen" />
In the only the layout attribute is required. This attribute, without the android namespace prefix, is a reference to the layout file you wish to inflate. You can also use android:id
to specify the id of the root view of the included layout; it will also override the id of the included layout if one is defined. Similarly, you can override all the layout parameters.
Upvotes: 0
Reputation: 6496
You need to pass in the layout resource ID instead. Replace R.id.restaurant_displayed_on_list
with R.layout.<name of your layout file here>
and you should be good to go.
Upvotes: 2