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.
The Class Hierarchy of the Button Class in Kotlin
XML Attributes of Button Widget
Used to specify the id of the view.
Used to the display text of the button.
Used to the display color of the text.
Used to the display size of the text.
Used to the display style of the text like Bold, Italic, etc.
Used to display text in Capital letters.
Used to set the background of the view.
Used to set the padding of the view.
Used to set the visibility of the view.
Used to specify the gravity of the view like center, top, bottom, etc
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.
Note: Following steps are performed on Android Studio version 4.0
Step 1: Create a new project
Click on File, then New => New Project.
Choose “Empty Activity” for the project template.
Select language as Kotlin.
Select the minimum SDK as per your need.
Step 2: Modify the strings.xml file
Navigate to the strings.xml file under the “values” directory of the resource folder. This file will contain all strings that are used in the Application. Below is the appropriate code.
<stringname="app_name">GfG | Button In Kotlin</string>
<stringname="message">Hello Geeks!! This is a Button.</string>
Step 3: 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.
Please Login to comment...