Android Button using Kotlin and XML With Examples

In this tutorial, we’ll learn how to create a Button in Android apps using Kotlin programming.

Android Button Overview

Android Button class extends TextView. Button is a UI widget that is used to get click interactions from the user to trigger an action in the application.

A button can be created in the XML layout as well as the Kotlin Activity class in the Android Studio Project.

Creating a Button in XML Layout

  • android:id is used to set the unique identifier on the Button.
  • The android:text is used to set the text inside the button. By default text is displayed in capital letters.
  • android:onClick is used to define the Kotlin function to be invoked in the activity when the button is clicked. It is a click listener.
  • The android:background is used to set the background color/drawable on the Button.

Tip: To prevent displaying all letters in captial, use the attribute android:textAllCaps="false"

For more details on how to customize your Buttons in XML Layout refer to the Android Buttons Tutorial.

Button Click Listeners

We can set button listeners programmatically too. Following are the two major listeners:

  1. setOnClickListener – triggers when a button is clicked.
  2. setOnLongClickListner – triggers when a button is pressed for a longer duration.

Following code snippets has the setOnClickListener set over a button.

The above code can be converted in a lambda expression to make it short.

Similarly, a setOnLongClickListener can be defined in the following manner.

In the above code, the last statement in each of the expressions is the return statement.

  • If the setOnLongClickListener returns true, it means that the setOnClickListener won’t be triggered.
  • If the setOnLongClickListener returns false, it means that the setOnClickListener will be triggered.

This is known as consuming events. The first case consumes the event.

Android Button using Kotlin

We’ll be developing an application that increments the counter of the TextView on a Button click. We will use Kotlin to create the button. We’ll also learn about the different Button click handlers.

1. Project Structure

Create a new Android Studio Project. Ensure that in the initial setup, you enable Kotlin Support. Once you’re done, following is the Project Structure that you shall see.

android-vector-drawable-project-structure

2. Kotlin Button Code

The activity_main.layout file looks like the following code.

We’ve used LinearLayout that holds the views linearly (horizontally or vertically).

It’s recommended to set the strings in the strings.xml file instead of hardcoding them. To fetch a string resource we use @string/name_of_string.

android-button-ids-xml

The function addOne(view: View) is defined in the MainActivity.kt Kotlin class.

The code for the MainActivity.kt class is given below.

Important Points:

  1. import kotlinx.android.synthetic.main.activity_main.* statement automatically gets the view IDs from the xml in our class. Hence saving us from using findViewById.
  2. The fun addOne(view: View) is triggered when the btnIncrementByOne is clicked. The (view: View) parameter must be defined in the function declaration.
  3. Create a Button programmatically and set it in the parent view(LinearLayout here) using the following code.
  4. Instead of calling member functions on the Button class, we can use apply{} lambda expression.
  5. The layoutParams is used to define the width and height of the button. The MATCH_PARENT sets the width/height equal to the linear layout. WRAP_CONTENT wraps the view to the size of the content.
  6. We can set the id programmatically under res | values | ids.xml.
  7. android-button-ids-xml
  8. We’ve defined the View.OnClickListener interface in our MainActivity.kt class. Hence we need to override its onClick() function.
  9. Inside the onClick function, we use the Kotlin when statement, which is equivalent to switch in other languages.
  10. For the onClick function to be triggered, you must register the setOnClickListener over the button with the interface using the context(this).

Output:

android button kotlin output

By admin

Leave a Reply

%d bloggers like this: