0

Snapshot of Xml: enter image description here

I just want to add same child LinearLayout into Parent LinearLayout multiple times as per user requirements using for loop with same contents as child(i have added some TextView and ImageView to child LinearLayout);

public class ListData extends AppCompatActivity {
    LinearLayout childLL,parentLL;
    TextView tName,tEmail,tCity;
    ImageView iImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_data);

    parentLL=(LinearLayout) findViewById(R.id.parentList);
    childLL=(LinearLayout) findViewById(R.id.childList);
    tName=(TextView) findViewById(R.id.listName);
    tEmail=(TextView) findViewById(R.id.listEmail);
    tCity=(TextView) findViewById(R.id.listCity);
    iImage=(ImageView) findViewById(R.id.listImage);

    for(int i=0;i<10;i++) {
        //parentLL.addView(childLL);
       //please Suggest code 
    }
}

}
1
  • use recycleview or list view instead of for loop Commented Feb 16, 2017 at 8:51

1 Answer 1

1

You can move childLayout to a separate layout file and add it manually. Let's the new layout file is called child_layout, the sample code could be like:

LayoutInflater inflater = getLayoutInflater();
for (int i = 0, i < maxSize, i++) {
    View v = inflater.inflate(R.layout.child_layout, null);
    //customise your child view here if needed.
    parentLL.addView(v);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.