VOOZH about

URL: https://www.geeksforgeeks.org/android/button-in-kotlin/

⇱ Button in Android - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Button in Android

Last Updated : 12 Jul, 2025

In Android applications, a Button is a user interface that is used to perform some action when clicked or tapped. It is a very common widget in Android and developers often use it. This article demonstrates how to create a button in Android Studio.

Class Hierarchy of the Button Class in Kotlin

kotlin.Any
↳ android.view.View
↳ android.widgets.TextView
↳ android.widget.Button

XML Attributes of Button Widget

XML Attributes

Description

android:idUsed to specify the id of the view.
android:textUsed to the display text of the button.
android:textColorUsed to the display color of the text.
android:textSizeUsed to the display size of the text.
android:textStyleUsed to the display style of the text like Bold, Italic, etc.
android:textAllCapsUsed to display text in Capital letters.
android:backgroundUsed to set the background of the view.
android:paddingUsed to set the padding of the view.
android:visibilityUsed to set the visibility of the view.
android:gravityUsed to specify the gravity of the view like center, top, bottom, etc

Step by Step Implementation of Button in Android

In this example step by step demonstration of creating a Button will be covered. The application will consist of a button that displays a toast message when the user taps on it.

Step 1: Create a new project

  1. Click on File, then New => New Project.
  2. Choose “Empty Activity” for the project template.
  3. Select language as Kotlin.
  4. Select the minimum SDK as per your need.

Step 2: Modify the activity_main.xml file

Add a button widget in the layout of the activity. Below is the code of the activity_main.xml file which does the same.

activity_main.xml:  

Layout:

👁 Button


Step 3: Accessing the button in the MainActivity file

Add functionality of button in the MainActivity file. Here describe the operation to display a Toast message when the user taps on the button. Below is the code to carry out this task. 

Output:


Comment

Explore