0

Aim: To have the if statement execute a onclick event through code.

I currently has this button in xml.

<Button 
   android:id="@+id/button1" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="Set Call" 
   android:onClick="makeCall"/>

I have tried R.id.button1.performClick(); but I get

Cannot invoke performClick() on the primitive type int

I have the below conditional to repeat if count is under 5.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    int count=0;

    if (count <= 5) {

       //make call                
       R.id.button1.performClick();

       count++;

       Toast toast=Toast.makeText(this, "Count is currently" + count++ + ", 
                                        repeating", Toast.LENGTH_LONG);
       toast.show();
    }
    else {
       // Toast Popup when call set button pressed
       Toast toast=Toast.makeText(this, "Call count complete, ending method" , 
                                                            Toast.LENGTH_LONG); 
       toast.show(); 

       count++;

    }
}
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
Parksie
  • 65
  • 1
  • 3
  • 10
  • 1
    Well, the message is pretty clear--an id is an integer, and indeed, you can't call `performClick` on it. If you want to call a button's handler programmatically, you need to actually get the resource as a button first. But that's backwards compared to what you say you want. Have you looked at any of the Android UI tutorials at all? – Dave Newton May 19 '12 at 21:20

1 Answers1

2

You need to get a reference to the button, then you can use it.

Button button1 = yourview.findViewById(r.id.button1);
button1.performClick();
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Barak
  • 16,318
  • 9
  • 52
  • 84