Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing OnClickListener for dynamically created buttons in Android

I dynamically created buttons through code rather than from XML.
The code is as below :

    dynamicview = (LinearLayout)findViewById(R.id.llayout);
     LinearLayout.LayoutParams lprams = new LinearLayout.LayoutParams(
             LinearLayout.LayoutParams.WRAP_CONTENT,
             LinearLayout.LayoutParams.WRAP_CONTENT);

     for(int i=0;i<nob;i++){
         Button btn = new Button(this);
         btn.setId(i+1);
         btn.setText("Button"+(i+1));
         btn.setLayoutParams(lprams);
         dynamicview.addView(btn);
     }


I am not finding a way in which I can implement OnClickListener to each of these buttons so that I can perform action based on the reference I get.
Can anyone help me in sorting out this issue. ?
Thanks in Advance,

like image 261
user1276092 Avatar asked May 20 '12 13:05

user1276092


3 Answers

   for(int i=0;i<nob;i++){
         Button btn = new Button(this);
         btn.setId(i+1);
         btn.setText("Button"+(i+1));
         btn.setOnClickListener(btnclick); <<<<<<<set click
         btn.setLayoutParams(lprams);
         dynamicview.addView(btn);
     }

And add this listner outside the any method and inside class

 OnClickListener btnclick = new OnClickListener() {

        @Override
        public void onClick(View view) {

            switch(view.getId()){
              case 1:
               //first button click
              break;
               //Second button click
              case 2:
              break;
              case 3:
               //third button click
              break;
              case 4:
               //fourth button click
              break;
            .
            .
            .
             default:
              break;
              }

        }
    };
like image 186
Samir Mangroliya Avatar answered Nov 14 '22 03:11

Samir Mangroliya


See the code below:

for(int i=0;i<nob;i++) {
    Button btn = new Button(this);
    btn.setId(i+1);
    btn.setText("Button"+(i+1));
    btn.setLayoutParams(lprams);
    final int index = i;
    btn.setOnClickListener(new OnClickListener() {
        void onClick(View v) {
            Log.i("TAG", "The index is" + index);
        }
    });
    dynamicview.addView(btn);
}

My example is pretty simple, but demonstrates how you can get the button index into the OnClickListeber. You can access any final field in the declared anonymous classes (e..g the OnClickListener).

like image 39
Boris Strandjev Avatar answered Nov 14 '22 02:11

Boris Strandjev


Use a List and add Buttons you create to that List

List<Button> list = new ArrayList<Button>();

Now add your button to that List

for(int i=0;i<nob;i++){
         Button btn = new Button(this);
         btn.setId(i+1);
         btn.setText("Button"+(i+1));
         btn.setLayoutParams(lprams);
         dynamicview.addView(btn);
         list.add(btn);
     }

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

for(Button btn : list){
btn.setOnClickListener(this);
}
like image 2
Arif Nadeem Avatar answered Nov 14 '22 04:11

Arif Nadeem