setonclicklistener in android code example

Example 1: setOnClickListener example kotlin

// set on-click listener
btn_click_me.setOnClickListener {
    Toast.makeText(this, "You clicked me.", Toast.LENGTH_SHORT).show()
}

Example 2: 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
             }
         });
     }

}

Tags:

Java Example