LinearLayout btnsContainer = new LinearLayout(getApplicationContext());
btnsContainer.setLayoutParams(new LinearLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
btnsContainer.setOrientation(LinearLayout.VERTICAL);
btnsContainer.setGravity(Gravity.CENTER);
//Crea botons dinamicamente.
for (int i = 0; i < 100; i++){
final LinearLayout buttonContainer = (LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.item_button,null);
ImageView btnImg = (ImageView) buttonContainer.findViewById(R.id.btn_image);
TextView btnTxt = (TextView) buttonContainer.findViewById(R.id.btn_text);
btnTxt.setText("mi Botón no." + i);
btnTxt.setBackgroundColor(getRandomColor());
btnImg.setImageResource(R.mipmap.ic_launcher);
buttonContainer.setTag(i);
buttonContainer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), " Listener botón " + v.getTag() + " id: " + v.getId(), Toast.LENGTH_SHORT).show();
}
});
//Va agregegando botones al contenedor.
btnsContainer.addView(buttonContainer);
}
//Crea contenedor para agregar contenedor de botones.
FrameLayout.LayoutParams paramsContainer = new FrameLayout.LayoutParams(400, 1500, Gravity.CENTER);
//Agrega contenedor con botones.
addContentView(btnsContainer, paramsContainer);