-1

How can i create a single click listener for multiple buttons which are created dynnamically? This is my code.

int cn = myary.length;
ScrollView sv = new ScrollView(this);
TableLayout ll = new TableLayout(this);
HorizontalScrollView hsv = new HorizontalScrollView(this);
TableRow tbrow0 = new TableRow(this);

    EditText tv0 = new EditText(this);
    tv0.setText("");
    tbrow0.addView(tv0);

    ll.addView(tbrow0);
    int j = 0;
    for (int i = 0; i < cn; i++) {
        TableRow tbrow = new TableRow(this);

        t1v = new Button(this);
        t1v.setText(myary[i]);
        t1v.setId(i+j);
        tbrow.addView(t1v);

        ll.addView(tbrow);
    }
    hsv.addView(ll);
    sv.addView(hsv);
    setContentView(sv);
Vettiyanakan
  • 7,957
  • 6
  • 37
  • 55

3 Answers3

3

You can create an inner class which implements View.OnClickLister, and then you can set it to every button of you:

Let's create a global object just to make @njzk2 happy:

private ClickListener mySingleListener = new ClickListener();

And let's set this single listener to all buttons of us:

t1v = new Button(this);
t1v.setTag(i);
t1v.setOnClickListener(mySingleListener);

And your ClickListener class is:

private class ClickListener implements View.OnClickListener {

    @Override
    public void onClick(View v) { 
         int position = (Integer) v.getTag(); 
         // Do click event here
    }

}
yahya
  • 4,810
  • 3
  • 41
  • 58
  • same comment as @Scelus : not helping, as in onClick you have no idea which btn have been clicked. – njzk2 Apr 12 '13 at 14:16
  • then set tag to buttons and get tag from onClick... I update my answer – yahya Apr 12 '13 at 14:21
  • then you don't need a new OnClickListener each time, you can reuse the same. – njzk2 Apr 12 '13 at 14:30
  • That is true :) But i was so lazy to create a global object and set it to all, so i just simply write it so :) – yahya Apr 12 '13 at 14:32
  • The question is quite specific : `a single click listener`, though – njzk2 Apr 12 '13 at 14:34
  • now it looks quite ok. (also, that's not to make me happy, that's to answer the question is the more precise fashion) – njzk2 Apr 12 '13 at 15:02
1

Create a class that implements the View.OnClickListener

class MyClickListener implements View.OnClickListener{

    @Override
    public void onClick(View v) {
        // put here what the click should do

    }

}

then attach objects from your class to your buttons

btn.setOnClickListener(new MyClickListener());
SceLus
  • 1,117
  • 13
  • 18
0

For example you have 3 buttons in main layout:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_switcher"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

<Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/btn1"
        android:onClick="onClick"
        />
<Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/btn2"
        android:onClick="onClick"
        />
<Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/btn3"
        android:onClick="onClick"
        />

</LinearLayout>

I your activity metod onClick handle events from three buttons. And we need to recognize that the button has been pressed.

 public void onClick(View view) { // Parameter 'view' used
    switch (view.getId())
    {
        case (R.id.btn1):
        {
            Toast.makeText(this, "Hello Button_1 pressed", Toast.LENGTH_LONG).show();
            break;
        }
        case (R.id.btn2):
        {
            Toast.makeText(this, "Hello Button_2 pressed", Toast.LENGTH_LONG).show();
            break;
        }
        case (R.id.btn3):
        {
            Toast.makeText(this, "Hello Button_2 pressed", Toast.LENGTH_LONG).show();
            break;
        }
    }

This is one example of how you can use the 'view'

jimpanzer
  • 3,470
  • 4
  • 44
  • 84