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 8877563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:29:52+00:00 2026-06-14T19:29:52+00:00

I am attempting to create multiple instances of a linearLayout. I created my layout

  • 0

I am attempting to create multiple instances of a linearLayout. I created my layout file (Below). I put it in its own layout file and from another file (main_activity.xml) I would like to add a dynamic number of Linear Layout instances within a scroll view. I know that I can programatically create the same linearlayout in my java code and loop through it but I would rather not (for two main reasons)

  1. Creating massive layouts in my java class is starting to make my code look messy :).
  2. I like the paradigm of having my layouts in XML and my logic in Java.

Is it possible for me to keep my logic in java and then just dynamically create/instantiate multiple “instances” of my Linear layout. My guess is that there is a simple solution but my newbie status is clouding my ability to solve it. For some reason my java code can find the LinearLayout but is not able to find the first TextView(@+id/submittedBy).

Thank you very much,
Craig

Java Logic

  ScrollView sv = (ScrollView) findViewById(R.id.existingCommentsScrollView);
    //DEBUGGING LOOP- REMOVE ME
    for(int i=0;i<30;i++){
        LinearLayout ll = (LinearLayout)findViewById(R.id.existingCommentLinearLayout);

        TextView submittedByTextView = (TextView) findViewById(R.id.submittedBy);
        submittedByTextView.setText("Craig " +i);

        TextView submittedDate = (TextView) findViewById(R.id.dateField);
        submittedDate.setText("SubmittedDate " +i);

        RatingBar showRating = (RatingBar) findViewById(R.id.userShowRating);
        showRating.setRating(3);

        RatingBar soundRating = (RatingBar) findViewById(R.id.userSoundRating);
        soundRating.setRating(1);

        EditText commentText = (EditText) findViewById(R.id.userCommentEditText);
        commentText.setText("THIS IS WHERE MY TEXT GOES FOR "+i+" I could add more too");

        sv.addView(ll);
    }
    sv.refreshDrawableState();

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="@drawable/linear_layout_border"
android:orientation="vertical"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:paddingTop="4dp"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

    <TextView
        android:id="@+id/submittedBy"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="center_horizontal"
        android:inputType="none"
        android:paddingBottom="1dp"
        android:paddingLeft="4dp"
        android:paddingRight="10dp"
        android:paddingTop="1dp"
        android:text="NameGoesHere"
        android:textColor="#E6E6E6"
        android:textSize="8sp" />

    <TextView
        android:id="@+id/dateField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/submittedBy"
        android:ems="10"
        android:paddingBottom="1dp"
        android:paddingLeft="4dp"
        android:paddingRight="10dp"
        android:paddingTop="1dp"
        android:text="DateGoesHere"
        android:textColor="#E6E6E6"
        android:textSize="8sp" />
</RelativeLayout>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:paddingTop="4dp"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

    <TextView
        android:id="@+id/userShowRatingTextView"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:gravity="center_horizontal"
        android:inputType="none"
        android:paddingBottom="1dp"
        android:paddingLeft="4dp"
        android:paddingRight="10dp"
        android:paddingTop="1dp"
        android:text="Show Rating"
        android:textColor="#E6E6E6"
        android:textSize="8sp" />

    <RatingBar
        android:id="@+id/userShowRating"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/userShowRatingTextView" />
</RelativeLayout>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:paddingBottom="4dp"
    android:paddingLeft="4dp"
    android:paddingRight="4dp"
    android:paddingTop="4dp"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

    <TextView
        android:id="@+id/userSoundRatingTextView"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toRightOf="@id/userShowRating"
        android:gravity="center_horizontal"
        android:inputType="none"
        android:paddingBottom="1dp"
        android:paddingLeft="4dp"
        android:paddingRight="10dp"
        android:paddingTop="1dp"
        android:text="Sound Rating"
        android:textColor="#E6E6E6"
        android:textSize="8sp" />

    <RatingBar
        android:id="@+id/userSoundRating"
        style="?android:attr/ratingBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/userSoundRatingTextView" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:inputType="none"
        android:paddingBottom="4dp"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:paddingTop="4dp"
        android:text="Comment Goes Here"
        android:textColor="#E6E6E6"
        android:textSize="12sp"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >

        <requestFocus />
    </EditText>
</RelativeLayout>

  • 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-06-14T19:29:53+00:00Added an answer on June 14, 2026 at 7:29 pm

    You don’t run into any exception? I’m referring to the last line in the loop, where you add the LinearLayout to the ScrollView in each iteration. You’re probably trying to inflate your layout file and then add it to the ScrollView like below:

    ScrollView sv = (ScrollView) findViewById(R.id.existingCommentsScrollView);
    // I assume this LinearLayout is the only child of the ScrollView above
    LinearLayout ll = (LinearLayout)findViewById(R.id.existingCommentLinearLayout);
    for(int i=0;i<30;i++){        
        // R.layout.the_layout_file is your layout from the question
        View inflatedView = getLayoutInflater().inflate(R.layout.the_layout_file, ll, false);
        TextView submittedByTextView = (TextView) inflatedView.findViewById(R.id.submittedBy);
        submittedByTextView.setText("Craig " +i);
    
        TextView submittedDate = (TextView) inflatedView.findViewById(R.id.dateField);
        submittedDate.setText("SubmittedDate " +i);
    
        RatingBar showRating = (RatingBar) inflatedView.findViewById(R.id.userShowRating);
        showRating.setRating(3);
    
        RatingBar soundRating = (RatingBar) inflatedView.findViewById(R.id.userSoundRating);
        soundRating.setRating(1);
    
        EditText commentText = (EditText) inflatedView.findViewById(R.id.userCommentEditText);
        commentText.setText("THIS IS WHERE MY TEXT GOES FOR "+i+" I could add more too");
    
        ll.addView(inflatedView);
    }
    

    Also, keep in mind that you’re adding a lot of views, something to avoid for a smooth interface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to create a class in vb.net from the below JSON example.
I am attempting following code to create multiple instances of a class at run-time
While attempting to create a replacement tablespace for USER, I accidentally created a datafile
I'm attempting to create a ListView adapter that has multiple types of views. I'm
I am currently saving a message to a single file from multiple activity classes
I am attempting to create my own view in android which is simply an
I'm attempting to create my own todo list using Javascript, Python and MongoDB. I'm
I am attempting to create multiple, serial writing sessions with AVAssetWriter. Once I've completed
I am attempting to set up multiple datasources in my standalone.xml file. I have
Im attempting to create a new operator :? on lists, which operates the same

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.