Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6732393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:38:42+00:00 2026-05-26T10:38:42+00:00

OK, So I understand how to use the include tag but I’ve run into

  • 0

OK, So I understand how to use the include tag but I’ve run into a problem.

Basically I want to have a layout defined in xml which has a couple of TextViews and an ImageView in it. I then want to iterate across an array and populate fields within the xml layout depending on whats in an array(which is populated on runtime). Thus making multiple copies of the xml layout and populating the fields with unique data. Now i’ve got no idea how you can re-use this LinearLayout in this way as the TextViews and ImageViews within it have a constant id and I need to make multiple copies of this layout.

Is there any way to inflate a resource and then make a copy of it, that would work… So

LinearLayout one = new LinearLayout(inflater.inflate(R.layout.home, container, false));

^ There is no constructor like that unfortunately.

The only other way is to do it all programatically but I would of preferred to have the properties of the views and the LinearLayout in xml rather than in the code. It’s like I want the LinearLayout to be a template which you can make copies of I guess… Really not sure if that’s possible.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:38:43+00:00Added an answer on May 26, 2026 at 10:38 am

    You can easily do this, you just have to break it down. First you load the layout that you want to insert your dynamic views into. Then you inflate your subview and populate it as many times as you need. Then you add the view to your parent layout, and finally set the content view of the activity to the parent view.

    Here’s an example:

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.main, null);
    
    for (int i = 0; i < 3; i++) {
        View custom = inflater.inflate(R.layout.custom, null);
        TextView tv = (TextView) custom.findViewById(R.id.text);
        tv.setText("Custom View " + i);
        parent.addView(custom);
    }
    
    setContentView(parent);
    

    here is the main.xml file that I am inserting into:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
    </LinearLayout>
    

    and here is the custom.xml view that I inflate, populate and dynamically insert:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
    
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >
    
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />
    
            <TextView
                android:id="@+id/text"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    
    </LinearLayout>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use jsoup-1.6.0,parse html tag string, that only include string content 0, but jsoup
I understand that if you want to include external packages you have to include
I can understand the use for one level of namespaces. But 3 levels of
I want to understand the use of xargs man in Rampion's code : screen
I understand how to use it, but the syntax of it bothers me. What
I understand that within same folder, I can use include() function for external PHP
I am trying to use the Django include tag for the first time and
I have a bunch of jsps that use the include directive to pull in
I want to understand how to use templating system and permalinks on php websites
I read a tutorial in Django site but I don't understand how to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.