0

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..

4 Answers 4

4

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
}

});

Sign up to request clarification or add additional context in comments.

Comments

2

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);
}

Comments

0

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);

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.