Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform click on button in Android SDK

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.

like image 507
ali Avatar asked Mar 05 '13 13:03

ali


People also ask

What is perform click in android?

performClick(); } }); This way the runnable will run only if the button is already loaded on the layout. Save this answer.

Which method is execute when we click on any view?

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.

What is android ImageButton?

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.


1 Answers

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.

like image 98
abbath Avatar answered Nov 16 '22 01:11

abbath