Can I click a button programmatically for a predefined intent?
You can click a button programmatically by using the button.performClick()
method.
android code to press a button automatically when it receives a certain order
We need a bit more to go on than this, which button do want to emulate being pressed when the event is triggered?
For example if you want to press the back button you can just call
onBackPressed();
inside your activity, other button actions might require more complex calls.
or if you want to press one of the buttons you have created inside your layout you can call
button.performClick();
Referring to programmatically created button and it's click event
Move btn.Click += (sender, e)
subscription inside for loop.
Even better - create one named method instead of creating many anonymous. E.g. Button_Click and subscribe to it:
btn = new Button(this);
btn.Click = Button_Click;
Inside that method you can cast sender object to Button and know which button was clicked.
UPDATE: here is complete code
const int rowsCount = 10;
const int columnsCount = 5;
int buttonsCount = rowsCount * columnsCount;
for (int i = 0; i < buttonsCount; i++)
AddButton();
I prefer not to use magic numbers in code :)
private void AddButton()
{
Button button = new Button(this);
button.Click += Button_Click;
// add text and other properties
main.AddView(button);
}
private void Button_Click(object sender, EventArgs e)
{
Button button = (sender as Button);
// use clicked button e.g. Console.WriteLine("Selected = {0}", button.Text);
}
Android - Simulate Home click
You can simply use an Intent for that:
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
How to simulate programmatically button click on background of Composite?
OK this is the main form :
package test.src;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class CompositeClicker
{
private static Display display;
private static Shell shell;
private static PComposite composite;
public static void main ( String [ ] args )
{
display = new Display ( );
shell = new Shell ( display );
shell.setLayout ( new GridLayout ( 1 , false ) );
shell.setLocation ( 100 , 50 );
shell.setSize ( 100 , 500 );
composite = new PComposite ( shell );
composite.setLayoutData ( new GridData ( SWT.FILL , SWT.FILL , true , true ) );
composite.setLayout ( new GridLayout ( 1 , false ) );
shell.open ( );
while ( !shell.isDisposed ( ) )
{
if ( !display.readAndDispatch ( ) )
display.sleep ( );
}
display.dispose ( );
}
}
And this is the PComposite class:
package test.src;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import javax.swing.SwingUtilities;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.widgets.Composite;
public class PComposite extends Composite
{
int x;
int y;
public PComposite ( Composite parent )
{
super ( parent , SWT.BORDER | SWT.EMBEDDED );
addPaintListener ( new PaintListener ( )
{
@Override
public void paintControl ( PaintEvent e )
{
Point p = new Point ( PComposite.this.getClientArea ( ).x + 11 , PComposite.this.getClientArea ( ).y + 12 );
Panel panel = new Panel ( );
panel.setBounds ( getShell ( ).getBounds ( ).x , getShell ( ).getBounds ( ).y + 25 , getShell ( ).getBounds ( ).width , getShell ( ).getBounds ( ).height );
SwingUtilities.convertPointToScreen ( p , panel );
x = p.x;
y = p.y;
try
{
trainer ( );
}
catch ( Exception exc )
{
exc.printStackTrace ( );
}
}
} );
addMouseListener ( new MouseAdapter ( )
{
@Override
public void mouseDown ( MouseEvent e )
{
System.out.println ( e.x + " " + e.y );
}
} );
}
private void trainer ( ) throws Exception
{
Robot trainer = new Robot ( );
trainer.mouseMove ( x , y );
int delay = 100;
int clicks = 1;
while ( clicks-- > 0 )
{
trainer.mouseMove ( x , y );
trainer.mousePress ( InputEvent.BUTTON1_MASK );
trainer.delay ( delay );
}
}
}
You have to use swing in your SWT application to success.
Related Topics
How to Get Data from Dialogfragment to a Fragment
Fragment Standard Transition Not Animating
Combining Two Png Files in Android
Android Tablayout Android Design
Writing Some Characters Like '<' in an Xml File
How to Get The Time Spent on an Application in Android Programmatically
Change Background of Edittext's Error Message
Notification Passes Old Intent Extras
Mediarecorder Issue on Android Lollipop
Inform Activity from a Broadcastreceiver Only If It Is in The Foreground
How to Get/Set Media Volume (Not Ringtone Volume) in Android
Android: Runonuithread Vs Asynctask
Audio Format for iOS and Android
Change Actionbarsherlock Background Color