Jayakarthik Appasamy
Jayakarthik Appasamy

Reputation: 129

dynamic button onclick event inside for loop

i'm using this code to create dynamic buttons within for loop..

LinearLayout linearLayout =  (LinearLayout)findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);

TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);

l.addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
button.setWidth(90);
button.setHeight(60);
l.addView(button);
linearLayout.addView(l);//if you want you can layout params linearlayout
}

now i want to add onclick event to each button based on the iterating i value.. can any one suggest me how to implement this... thanks in advance..

Upvotes: 0

Views: 6224

Answers (4)

Shankar Agarwal
Shankar Agarwal

Reputation: 34765

add button.setOnClickListener(this); in the forloop

and add these code below oncreate():::

@Override
public void onClick(View v){
switch(v.getId()){
case 0:
// click action for first btn
break;
case 1: 
// click action for 2nd btn
break;
soon upto 
case 9: 
// click action for 9th btn
break;
}
}

also ypur activity must implement onClickListerner.

Upvotes: 0

5hssba
5hssba

Reputation: 8079

create an arraay of buttons...

Button[] buttons=new Button[10];

and instead of this line

Button button = new Button(this);

in your for loop..use

 button[i] = new Button(this);

and in the same loop set your onclicklistener like this..and based on the question you were asking add onclick event to each button based on the iterating i value i think you have same onclicklistener for all buttons..

    button[i].setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
        //your onclicklistener code
}

});

Upvotes: 4

Arif Nadeem
Arif Nadeem

Reputation: 8604

Create a List of Button's then add each Button to that list after its created...

List<Button> list = new ArrayList<Button>();
for (int i=0 ; i<10; i++){
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);

TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);

l.addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
button.setWidth(90);
button.setHeight(60);
l.addView(button);
linearLayout.addView(l);//if you want you can layout params linearlayout
list.add(button);
}

Then use advanced for loop to iterate through the list and add click listener for each Button..

for(Button btn : list){
btn.setOnClickListener(this);
}

Upvotes: 2

Phix
Phix

Reputation: 9890

Outside your loop create a new View.OnClickListener:

OnClickListener ocl = new OnClickListener(){
    @Override
    public void onClick(View v){
        switch(v.getId()){
        case 1:
            // click action
            break;
        case 2: 
            // click action
            break;
        }
        // ...etc
    }
}

Based on the ID of your button, in this case the iteration of i, you can perform various actions. This can be anything, however, like an if...else block on the text of the button or a tag you set.

Then inside your loop, before you addView(l), assign it:

button.setOnClickListener(ocl);

Upvotes: 0

Related Questions