Kotlin Android ButtonAndroid Button is a push button used to perform events on its click. It is a UI component comes under the android.widget.Button class. To learn more about Android Button refers to Android Button Example Using Kotlin, we can perform events on Android Button though different ways, using: 1. Implement the setOnClickListener of Button 2. Implement the View.OnClickListner and override its function 3. Adding the onClick attribute of Button in layout file and implement its function. 4. Create a Button programmatically and set it on the layout Kotlin Android Button ExampleIn this example, we will create the Button and performs event on them. Clicking on the Button, display a toast message. activity_main.xmlAdd the three Button from the Widgets palette in the activity_main.xml layout file. Its code is given below. The Button of id button3 added the onClick attribute and its function name is implemented in MainActivity class file. MainActivity.ktAdd the following code in the MainActivity.kt class. In this class, we implement the setOnClickListener listener on the button, implements the OnClickListener of View class (View.OnClickListener) and override its function onClick. In this class, we also create a Button programmatically (button4), define its properties and set it on the layout. Output: Next Topic# |