Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I click a button programmatically for a predefined intent?

I need the button click of the intent ACTION_SEND. Here there is no need of displaying UI. Can I get the "Send" button click from the MMS-SMSProvider in Android?

like image 999
info Avatar asked Apr 18 '11 10:04

info


People also ask

How to click button programmatically in Android studio?

You can click a button programmatically by using the button. performClick() method. Utilizing a wide range of different examples allowed the Android Studio Press Button Programmatically problem to be resolved successfully.

How to click a button by code Android?

To define the click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event.


3 Answers

You can click a button programmatically by using the button.performClick() method.

like image 178
Nirav Bhandari Avatar answered Oct 16 '22 16:10

Nirav Bhandari


If your button includes any animation, you'll need to perform the click and then invalidate each step after performClick. Here's how:

 button.performClick();
 button.setPressed(true); 
 button.invalidate(); 
 button.setPressed(false); 
 button.invalidate(); 

On occasion I've also had to introduce delay to get the animation to show. Like this:

 //initiate the button
 button.performClick();
 button.setPressed(true); 
 button.invalidate(); 
 // delay completion till animation completes
 button.postDelayed(new Runnable() {  //delay button 
     public void run() {  
        button.setPressed(false); 
        button.invalidate();
        //any other associated action
     }
 }, 800);  // .8secs delay time
like image 37
PeteH Avatar answered Oct 16 '22 16:10

PeteH


button.callOnClick();

this one can also be used

like image 44
Flash Avatar answered Oct 16 '22 16:10

Flash