As soon as the layout is created I want a button to be clicked automatically and I use button.performClick()
for that.
The problem is that it doesn't work. It looks like I can't do that during the onCreate, onStart, onResume
method. At what point is the button and its events created, so I can perform a click on it?
This
@Override
protected void onResume() {
super.onResume();
mybutton.performClick();
}
doesn't work.
performClick(); } }); This way the runnable will run only if the button is already loaded on the layout. Save this answer.
In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.
Android ImageButton is a user interface widget which is used to display a button having image and to perform exactly like button when we click on it but here, we add an image on Image button instead of text. There are different types of buttons available in android like ImageButton, ToggleButton etc.
This worked for me in a similar case:
mybutton.post(new Runnable(){
@Override
public void run() {
mybutton.performClick();
}
});
This way the runnable will run only if the button is already loaded on the layout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With