How to simulate a button click using code?

190,616

Solution 1

there is a better way.

View.performClick();

http://developer.android.com/reference/android/view/View.html#performClick()

this should answer all your problems. every View inherits this function, including Button, Spinner, etc.

Just to clarify, View does not have a static performClick() method. You must call performClick() on an instance of View. For example, you can't just call

View.performClick();

Instead, do something like:

View myView = findViewById(R.id.myview);
myView.performClick();

Solution 2

Just to clarify what moonlightcheese stated: To trigger a button click event through code in Android provide the following:

buttonName.performClick();

Solution 3

you can do it this way

private Button btn;
btn = (Button)findViewById(R.id.button2);
btn.performClick();

Solution 4

Just write this simple line of code :-

button.performClick();

where button is the reference variable of Button class and defined as follows:-

private Button buttonToday ;
buttonToday = (Button) findViewById(R.id.buttonToday);

That's it.

Solution 5

Android's callOnClick() (added in API 15) can sometimes be a better choice in my experience than performClick(). If a user has selection sounds enabled, then performClick() could cause the user to hear two continuous selection sounds that are somewhat layered on top of each other which can be jarring. (One selection sound for the user's first button click, and then another for the other button's OnClickListener that you're calling via code.)

Share:
190,616

Related videos on Youtube

sam
Author by

sam

Updated on May 14, 2022

Comments

  • sam
    sam 16 days

    How can I trigger a button click event using code in Android? I want to trigger the button click programmatically when some other event occurs.

    Same Problem I am Facing

    public void onDateSelectedButtonClick(View v){
        /*Something  Alarm Management 
        http://www.java2s.com/Code/Android/Core-Class/Alarmdemo.htm
        copied code from this site*/
    }
    

    Button code:

    <Button
        android:onClick="onDateSelectedButtonClick"
        android:text="Set notification for this date" />
    

    But I want to call that function OnLoadLayout without OnClickEvent

    • Vladimir Ivanov
      Vladimir Ivanov over 11 years
      can you please specify the problem? why do you want that? Do you want to trigger some code to be executed?
    • sam
      sam over 11 years
      yes, i want to trigger the code to be executed for the button click.
  • Peter Ajtai
    Peter Ajtai over 10 years
    But what is the View for Android's back button?
  • Pratik Popat
    Pratik Popat over 10 years
    how can get back button view?
  • moonlightcheese
    moonlightcheese over 10 years
    there is no "View" for the back button on your phone. you need to use key listeners to get those button presses. that's a different issue altogether.
  • Peter Ajtai
    Peter Ajtai over 10 years
    @moon - If you want to have people notified of your comments, start them with "@" and the first few letters of the user who you want notified.... (authors of the commented on post automatically get notified, but commenters do not)
  • Alex Semeniuk
    Alex Semeniuk about 9 years
    To simulate BACK button press you can just call onBackPressed() from your activity code (added in API8)
  • Lukasz Wiklendt
    Lukasz Wiklendt over 8 years
    The code shown in this answer is a little confusing. Just to clarify, View does not have a static performClick() method. You must call performClick() on an instance of View.
  • moonlightcheese
    moonlightcheese over 8 years
    it's only confusing if you don't know what you're doing.
  • Admin
    Admin over 6 years
    is correct, because button not is a button, works, because button is a view, all layers (textview/scroll/button... ) extends view, view.performClick(), only invoke click into view,
  • Syed Hamza Hassan
    Syed Hamza Hassan over 3 years
    Is there any way for performing click on Google Maps Marker ?