5

I have added some buttons with the following lines:

for (int i=0; i<XML.size(); i++) {
//add button
ToggleButton b = new ToggleButton(this); 
// Setting the parameters
lefttextv.setLayoutParams(lleft); 
b.setLayoutParams(bright);
//customize button
    b.setOnClickListener(this);
b.setId(id_button);
System.out.println(id_button);
b.setHeight(100);
b.setWidth(200);
// Adding to the RelativeLayout as a child
layouth.addView(lefttextv);
layouth.addView(b);
    id_button++;  
    }

But how can I get the OnClick() methods for those? I already implemented View.OnClickListener with this method:

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId())
    {
    case id_button: Log.d("Button 0","Button 0 pressed);
        break;
    }
}

But this does not work, how do I get the Id?

Mokkapps
  • 2,028
  • 9
  • 42
  • 67
  • 1
    Does `id_button` always 0? If not why are you checking for 0 in `case` statement? Consider not using magical constants next time. – olshevski Sep 24 '12 at 12:03
  • 1
    don't you have to call setOnClickListener at some point ? (also, setId does nothing, i think) – njzk2 Sep 24 '12 at 12:08
  • the creation of the buttons is in a for-loop and there are created more than one button. – Mokkapps Sep 24 '12 at 12:15
  • Why not replace id_button with i? An id is meant to be unique not staying the same... – FabianCook Sep 24 '12 at 12:37

5 Answers5

7

b is the view, if your onClick method is in your main class just use b.setOnClickListener(this); and let your activity implement onClickListener and there you have it. Or do the usual way you set tour listeners.

The id is used for xml reference, the object is created and your using this id to reference, in your case you created the view b with all the properties of a ToggleButton. It is the view.

Instead if using v.getId() just useif(v == b)

FabianCook
  • 20,269
  • 16
  • 67
  • 115
  • OK but I use a for loop and create more buttons and I do not know how to check for the different IDs in my switch/case statement... switch (v.getId()){case R.id.id_button: Log.d("ONCLICK","Button gedrückt");} is not possible because I set the ID with this line b.setId(id_button); – Mokkapps Sep 24 '12 at 12:16
  • Add the views to an ArrayList... use a view group to go through them and check which on it id. – FabianCook Sep 24 '12 at 12:32
  • And in that case if id_button is an incrementing number that represents each button why not just use Log.d("Button " + v.getId());? – FabianCook Sep 24 '12 at 12:33
  • I think an ArrayList would be your best bet, since each button has a different function – FabianCook Sep 24 '12 at 12:35
6

Just use b.setOnClickListener(this), where this refers to the class that implements OnClickListener.

Edit:

You can use ID resources to identify your buttons. See here for more information. You can assign those IDs to your programatically generated views and check for them in your switch/case statement.

Alternatively as @SmartLemon said, you can just check if (v == b), then you don't need to bother with IDs.

Jimbali
  • 2,065
  • 1
  • 20
  • 24
  • ok this works fine for one button, but I create more buttons in a for loop with different IDs. How can I realize this? – Mokkapps Sep 24 '12 at 12:04
  • OK but I do not know how to check for them in my switch/case statement... `switch (v.getId()){case R.id.id_button: Log.d("ONCLICK","Button gedrückt");}` is not possible because I set the ID with this line `b.setId(id_button);` – Mokkapps Sep 24 '12 at 12:13
  • You must set the ID of the button to use an ID resource. These are defined in a file like `res/values/ids.xml` that you create. Then they will appear in your `R.id` class. See the link I mentioned: http://developer.android.com/guide/topics/resources/more-resources.html#Id – Jimbali Sep 24 '12 at 12:17
  • I have created the file and of course it works. But I do not know how many IDs I have to create it depends on the for-loop, can be 3 or 10 buttons...and every button has a different onclick action – Mokkapps Sep 24 '12 at 12:26
  • In that case you should create the maximum number of IDs that you might need. – Jimbali Sep 24 '12 at 12:47
3

u have set id of the button as id_button right?

add onCreate Method:

 b1.setOnClickListener(this);    

Use that id in onClick method as below:

@Override
 public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId() == R.id.id_button)
    {
    Log.d("Button 0","Button 0 pressed);
        break;
    }
}
FabianCook
  • 20,269
  • 16
  • 67
  • 115
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
  • The `R` file is a generated file, generated before run time, hes not using R.id.id_button hes using id_button. Its just a local variable. – FabianCook Sep 24 '12 at 12:08
  • he has to use the id. Yes, i know R is a generated file. But he can use R.id. there is no problem because the id is stored in R before the user clicks the button.... – Avadhani Y Sep 24 '12 at 12:11
  • But he has the local variable already... would be silly to reference another file for something already there. That then brings up the next fact, why use v.getId and b.getId when you can just compare v to b? – FabianCook Sep 24 '12 at 12:14
  • break inside if condition ? – Tejasvi Hegde Nov 14 '14 at 05:02
3

As you implement the onClickListener in your activity class you can pass the current object using this keyword to register the click listener for the component.

class MyActivity extends Activity implements OnClickListener{
     private static final int id_button = 0;

     public void onCreate(Bundle b){
        //add button
        ToggleButton b = new ToggleButton(this); 
        // Setting the parameters
        lefttextv.setLayoutParams(lleft); 
        b.setLayoutParams(bright);
        //customize button
        b.setId(id_button);
        System.out.println(id_button);
        b.setHeight(100);
        b.setWidth(200);
        // Adding to the RelativeLayout as a child
        layouth.addView(lefttextv);
        layouth.addView(b);
        b.setOnClickListener(this);
     }

     @Override
     public void onClick(View v) {
        // TODO Auto-generated method stub
       switch (v.getId()){
       case 0: Log.d("Button 0","Button 0 pressed);
            break;
       }
    }
}
Pratik
  • 30,639
  • 18
  • 84
  • 159
1

change like this..

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    int i= b.getId();
    switch (v.getId())
    {
        case i:
            Log.d("Button 0","Button 0 pressed");
            break;
    }
}
Baz
  • 36,440
  • 11
  • 68
  • 94
Charan Pai
  • 2,288
  • 5
  • 32
  • 44