To add a Button
dynamically to a ViewGroup
, you must create the Button
and assign the parameters of the layout that are of the type that contains it. As I'm seeing, you're creating several LinearLayout
, not Button
in your code.
LinearLayout mlayout = new LinearLayout(this);
mlayout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
mlayout.addView(row);
}
This previous code causes you to create a LinearLayout
as root
that will contain 3 LinearLayouts
children. Now, if for each row (because the father defined vertical orientation), you want to add a button, you can do the following:
LinearLayout mlayout = new LinearLayout(this);
mlayout.setOrientation(LinearLayout.VERTICAL);
Each row will have a child container LinearLayout
which in turn will contain a Button
:
for (int i = 0; i < 3; i++) {
LinearLayout row = new LinearLayout(this);
Button btnInRow = new Button(this);
LinearLayout.LayoutParams lllp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lllp);
btnInRow.setLayoutParams(lllp);
btnInRow.setId(/* Aqui agregas cualquier entero positivo */);
btnInRow.setOnClickListener(myClickBtn);
row.addView(btnInRow);
mlayout.addView(row);
}
Keep in mind that by not assigning orientation
to row
, you are defining the orientation horizontal by default. You can add any property of the Button
object with the reference of btnInRow
, text , color , etc ...
Now let's go to the case. The id
of a view is an identifier of it, it does not represent the position in the Layout
that you want. So when you click and try to get the id, unless you have done a setId
with the specified case, it will take the statement as true or false in switch/case
. The id of the button must be unique for the root, but it can be the same as other id references, as long as two views do not have the same id in a root. This means that you should not set the same id, in the cycle for example, for the 3 created views, since you will not be able to take the correct reference when looking for it in root
with the same id .
You can do the following if you want to represent a posicion
in layout
, of each created view:
for (int i = 0; i < 3; i++) {
. . .
btnInRow.setId(/* Aqui agregas cualquier entero positivo unico para el root (no obligatorio)*/);
btnInRow.setTag(i); // donde i representa la posicion
btnInRow.setOnClickListener(myClickBtn);
. . .
}
and in your onClick
you get the Click of the button with the id if you have a global arrangement or a reference to the created ids, such as a list or the tag (the position) :
View.OnClickListener myClickBtn = new View.OnClickListener() {
int position = (int) boton.getTag();
public void onClick(View v) {
switch (position) {
case 0:
Toast.makeText(MainActivity.this, "Boton 1", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(MainActivity.this, "Boton 2", Toast.LENGTH_SHORT).show();
break;
case 2:
// it was the second button
Toast.makeText(MainActivity.this, "Boton 3", Toast.LENGTH_SHORT).show();
break;
}
}
};