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

  • Home
  • SEARCH
  • 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 6046063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:11:44+00:00 2026-05-23T07:11:44+00:00

I want to know whether it is possible to use an xml layout file

  • 0

I want to know whether it is possible to use an xml layout file to define the content of a view dynamically from within the code. When we start an activity we pass it the xml layout to use with the method call setContentView(R.layout.main); but is it possible to use an xml layout file to define a dynamically created ViewGroup such as LinearLayout?

I have a layout xml which shows a score table for a game. Each score that is displayed on this screen needs to be dynamically added via code. I know that it is possible within the code to create a ViewGroup for this score and populate it with all the things I need to make a single score, and then do this every time for each score, and then add them all to the existing UI structure, already defined in the xml layout. What I would like to know is if it is possible to use another xml file to do this?

For example, a layout xml file:

<LinearLayout android:id="@+id/top">
  <LinearLayout android:id="@+id/column_heading"/>
</LinearLayout>

In another xml layout file is something like:

<LinearLayout android:id="@+id/row">
  <TextView/>
  <TextView/>
  <TextView/>
  <TextView/>
</LinearLayout>

Within the code I would like to do something like the following:

LinearLayout top = (LinearLayout)findViewById(R.id.top);
for (int i = 0; i < num_of_rows; i++) {
  LinearLayout row = new LinearLayout(this);
  row.setContentView(R.layout.row);  //where R.layout.row is the second layout above
  // ... dynamically change values as needed
  top.addView(row);
}

However .setContentView(...) is not a valid method of LinearLayout. Is there another way to do this? I know I could do it all by code, but that’s rather messy and this way would seem to be very tidy and rational..

  • 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-23T07:11:44+00:00Added an answer on May 23, 2026 at 7:11 am

    You should use LayoutInflater for this. Here is a short example

    LinearLayout top = (LinearLayout)findViewById(R.id.top);
    for (int i = 0; i < num_of_rows; i++) {
        LayoutInflater inflater = LayoutInflater.from(this);
        LinearLayout row = (LinearLayout)inflater.inflate(R.layout.row, null);    
        // ... dynamically change values as needed
        top.addView(row);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know whether it is possible to use a SELECT statement
I just want to know, whether it is possible for me to add zoom
I am using following code and want to know whether we require to set
I am not concerned about other kinds of attacks. Just want to know whether
I don't care what the differences are. I just want to know whether the
I want to know, at run time, whether I'm running on 32 bit or
He want's to know our opinions on whether we should have flexitime, allow non-work
I want to know how to use variables for objects and function names in
I want to know whether one session is been created for each IP address
I know I can use Debugger.IsAttached to detect whether a debugger is attached, but

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.