events and listeners in java android code example

Example 1: button listener android

public class MyActivity extends Activity {

     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.my_layout_id);

         final Button button = (Button) findViewById(R.id.my_cool_button);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // your handler code here
             }
         });
     }

}

Example 2: onclick listener android

Button myButton;
awesomeButton = new Button(this);
awesomeButton.setOnClickListener(new View.OnClickListener() {
	@Override
    public void onClick(View v) {
    	startActivity(new Intent(MainActivity.this,NewActivity.class));
    });
}

Tags:

Java Example