How to simulate a button click using code?
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.)
Related videos on Youtube
sam
Updated on December 12, 2020Comments
-
sam over 3 years
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 over 13 yearscan you please specify the problem? why do you want that? Do you want to trigger some code to be executed?
-
sam over 13 yearsyes, i want to trigger the code to be executed for the button click.
-
-
Peter Ajtai over 12 yearsBut what is the View for Android's back button?
-
Pratik Popat over 12 yearshow can get back button view?
-
moonlightcheese over 12 yearsthere 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 over 12 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 about 11 yearsTo simulate BACK button press you can just call
onBackPressed()
from your activity code (added in API8) -
Lukasz Wiklendt over 10 yearsThe 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 over 10 yearsit's only confusing if you don't know what you're doing.
-
Admin over 8 yearsis 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 over 5 yearsIs there any way for performing click on Google Maps Marker ?