Invoking click event on all the views (Buttons, Layout etc.,) through code can be done so easily.

Method 1

Call performClick() function on the view for which you want to perform click event

Button btnSend = findViewById(R.id.btnSend);
LinearLayout layoutContainer = findViewById(R.id.container);

// perform click on button
btnSend.performClick();

// perform click on Linear Layout
layoutContainer.performClick();
Author

Ravi is hardcore Android programmer and Android programming has been his passion since he compiled his first hello-world program. Solving real problems of Android developers through tutorials has always been interesting part for him.

  • naani

    But Not Working in Some Areas
    My Code Reff : http://blog.blundell-apps.com/notification-for-a-user-chosen-time/
    final Button Sel = (Button) findViewById(R.id.selectButton);
    Sel.performClick();

    the Function is

    public void onDateSelectedButtonClick(View view){
    //Toast.makeText(this, “Notification set for: “, Toast.LENGTH_SHORT).show();

    Calendar c = Calendar.getInstance();

    int Year = c.get(Calendar.YEAR);
    int Month = c.get(Calendar.MONTH) ;
    int Day = c.get(Calendar.DAY_OF_MONTH) ;
    c.set(Year, Month, Day);
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    scheduleClient.setAlarmForNotification(c); /* this Part is Not woriking on Perform Click */
    // Notify the user what they just did
    Toast.makeText(this, “Notification set for: ” + Day + “/” + (Month + 1) + “/” + Year, Toast.LENGTH_SHORT).show();
    }

    Error

    09-04 14:02:37.167 1720-1720/com.Shanvitha.alarm3.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.Shanvitha.alarm3.app, PID: 1720
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Shanvitha.alarm3.app/com.Shanvitha.alarm3.app.MainActivity}: java.lang.IllegalStateException: Could not execute method of the activity
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.IllegalStateException: Could not execute method of the activity
    at android.view.View$1.onClick(View.java:3823)
    at android.view.View.performClick(View.java:4438)
    at com.Shanvitha.alarm3.app.MainActivity.onCreate(MainActivity.java:41)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at android.view.View$1.onClick(View.java:3818)
    at android.view.View.performClick(View.java:4438)
    at com.Shanvitha.alarm3.app.MainActivity.onCreate(MainActivity.java:41)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
    at com.Shanvitha.alarm3.app.ScheduleClient.setAlarmForNotification(ScheduleClient.java:62)
    at com.Shanvitha.alarm3.app.MainActivity.onDateSelectedButtonClick(MainActivity.java:66)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at android.view.View$1.onClick(View.java:3818)
    at android.view.View.performClick(View.java:4438)
    at com.Shanvitha.alarm3.app.MainActivity.onCreate(MainActivity.java:41)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)

  • Tohin sarker

    What is the work of this code?? i didnt understand plz tell me

    • Mohammad Babul

      suppose if have a layout in there are couples of view u want to set a on click listenener all over the view and all over the space of layout screen then use the
      LinearLayout layoutContainer = findViewById(R.id.container) for layout initilize
      and then set onclick listener in this way layoutContainer.performClick();