Get Button ID

1

I'm doing an app where I insert a series of buttons programmatically, this is my code and it generates it perfectly well. Where I have doubts is how to get the id of each button and show a msg that has been clicked on that button I hope you can help me thanks.

Button boton = new Button(this);

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

    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.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        mlayout.addView(row);
    }
    setContentView(mlayout);

    boton.setOnClickListener(myClickBtn);
}

View.OnClickListener myClickBtn = new View.OnClickListener() {
    int position = (int) boton.getId();

    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;
        }
    }
};
    
asked by Javier Irepan 18.05.2018 в 21:26
source

3 answers

1

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;
        }
    }
};
    
answered by 18.05.2018 / 22:25
source
-1

Use the getId() method. Returns the id as an integer so you can compare it with the id of the resources.

It is very convenient to use a switch statement like this:

public void onClick(View v) {
    switch (v.getId()) {
        case R.id.boton1:
            // accion
            break;
        case R.id.boton2:
            // accion
            break;
        case R.id.boton3:
            // accion
            break;
    }
}
    
answered by 18.05.2018 в 22:16
-1

I think it would be best to do it with the class LayoutInflater

View v = getLayoutInflater.inflate(R.layout.tu_xml, null);

.. and if you want to manipulate some object view as is your case a Button.

Button b = v.findViewById(R.id.id_de_tu_boton);
    
answered by 19.05.2018 в 05:36