user3364217
user3364217

Reputation: 67

onClickListener dynamically ANDROID

I have created dynamic buttons with the follow code and I want to listen them. The number of buttons created are undefined because the variable drawView.getNumeroMallas() is a dynamic number and can change each time. I need to know each index "i" for every button when I listen them. How can I implement the onClickListener dynamically? Thank you.

         LinearLayout buttonsLayout = (LinearLayout)findViewById(R.id.linearlayoutUp);

        for(int i=0;i<drawView.getNumeroMallas();i++){

            Button buttonMalla = new Button(this);
            Button buttonRotar = new Button(this);
            buttonMalla.setText("Malla "+(i+1));
            buttonsLayout.addView(buttonMalla);
            buttonsLayout.addView(buttonRotar);
        }

Upvotes: 0

Views: 100

Answers (5)

Ratul Ghosh
Ratul Ghosh

Reputation: 1500

Try this-

    LinearLayout buttonsLayout = (LinearLayout)findViewById(R.id.linearlayoutUp);
    for(int i=0;i<drawView.getNumeroMallas();i++){

        Button buttonMalla = new Button(this);
        Button buttonRotar = new Button(this);
        buttonMalla.setText("Malla "+(i+1));
        buttonMalla.setTag("M"+i);
        buttonRotar.setTag("R"+i);
        buttonMalla.setOnClickListner(dynamicButtonListner);
        buttonRotar.setOnClickListner(dynamicButtonListner);
        buttonsLayout.addView(buttonMalla);
        buttonsLayout.addView(buttonRotar);
    }

//outside of any method write this - 
private OnClickListner dynamicButtonListner = 
new OnClickListner(){
     onClick(View v)
            {
              String identifier = (String)v.getTag();
              if(identifier.chartAt(0)=='M')
              {
                 // malla button
                 int i = Integer.parseInt(identifier.chartAt(1));
              }
              else
              {
               //rotor button
               int i = Integer.parseInt(identifier.chartAt(1));
              }
            }      };

Upvotes: 1

user3232094
user3232094

Reputation: 56

By this way, you have the onClickListener for the two buttons but if you want only the onClickListener of one tyoe of button, remove the other one,

 for(int i=0;i<drawView.getNumeroMallas();i++){

            Button buttonMalla = new Button(this);
            Button buttonRotar = new Button(this);
            buttonMalla.setText("Malla "+(i+1));
            buttonMalla.setId(i+1);
            buttonRotar.setText("Rotar "+(i+1));
            buttonRotar.setId(i+1);
           // buttonsLayout.addView(buttonMalla);
            //buttonsLayout.addView(buttonRotar);

            final int index = i+1;

            buttonMalla.setOnClickListener(new OnClickListener() {
               public void onClick(View v) {
                 Log.i("TAG1", "The index is" + index);


                    }
                });
                buttonsLayout.addView(buttonMalla);

           buttonRotar.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                  Log.i("TAG2", "The index is" + index);
                            }
                        });
                 buttonsLayout.addView(buttonRotar);
            }

            }

Upvotes: 0

Lucian Novac
Lucian Novac

Reputation: 1265

on your for loop put:

buttonMalla.setOnClickListener(new OnCellClickListner(....what you need here as parameter of constructor));
buttonRotar .setOnClickListener(new OnCellClickListner(....what you need here as parameter of constructor));

class for oncellclicklistener here:

class OnCellClickListner implements OnClickListener {



 public OnCellClickListner(...){
          //initialize here }
    public void onClick(View arg0) {
    //do your job here

    }} 

Upvotes: 0

Dilip Suvagiya
Dilip Suvagiya

Reputation: 394

Try this one:

View.OnClickListener getOnClickDoSomething(final Button button)  {
    return new View.OnClickListener() {
        public void onClick(View v) {
            button.setText("text now set.. ");    
        }
    };
}

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

Assign a single click listener to every buttons you are creating dynamically and also assign i value as an ID to each button, something like:

buttonMalla.setOnClickListener(new myClickListener());
buttonMalla.setId(i);

You just have to check which button is called inside onClick().

For example:

class myClickListener implements OnClickListener {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()) {
          case 1:
              // button with ID==1 is clicked
             break;
        }
    }

Upvotes: 6

Related Questions