Inflate layout programmatically within another layout

You can implement this like below:

LayoutInflater linf;
LinearLayout rr;

linf = (LayoutInflater) getApplicationContext().getSystemService(
        Context.LAYOUT_INFLATER_SERVICE);
linf = LayoutInflater.from(activity.this);

rr = (LinearLayout) findViewById(R.id.container_destacado);

for (int i = 1; i < NoOfTimes; i++) {

    final View v = linf.inflate(R.layout.item, null);
    rr.addView(v);
}

You could use something like

LayoutInflater inflater = LayoutInflater.from(context);

//to get the MainLayout
View view = inflater.inflate(container_destacado, null);
...
//Avoid pass null in the root it ignores spaces in the child layout

View inflatedLayout= inflater.inflate(R.layout.yourLayout, (ViewGroup) view, false);
containerDestacado.addView(inflatedLayout);

In some point you should gain access to your inflater inside your activity, you can call it with this code:

LayoutInflater li = LayoutInflater.from(context);

Where context is this or this.getActivity() if it is a Fragment. Then inflate your layour with:

View layout = li.inflate(R.layout.your_layout, null, false);

Then use addView(layout) to your container_destacado:

View containerDestacado = li.inflate(R.layout.container_destacado, null, false);
containerDestacado.addView(layout);

Hope it helps.


ll = (LinearLayout) findViewById(R.id.container_destacado);  // ll is the layout where your inflated layout will be added 
linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int pos = 0;
    while (pos < noOfTimes) 
    {
        View myView = linflater.inflate(R.layout.item, null); //here item is the the layout you want to inflate 
        myView.setId(pos);
        /*
           You can change TextView text and ImageView images here e.g.
           TextView tv = (TextView)myView.findViewById(R.id.title_N1);
           tv.setText(pos);

        */
        pos++;
        ll.addView(myView);
    }