Open In App
Related Articles

Button in Android

Improve Article
Save Article
Like Article

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

The Class Hierarchy of the Button Class in Kotlin

XML Attributes of Button Widget

XML Attributes


android:id Used to specify the id of the view.
android:text Used to the display text of the button.
android:textColor Used to the display color of the text.
android:textSize Used to the display size of the text.
android:textStyle Used to the display style of the text like Bold, Italic, etc.
android:textAllCaps Used to display text in Capital letters.
android:background Used to set the background of the view.
android:padding Used to set the padding of the view.
android:visibility Used to set the visibility of the view.
android:gravity 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

  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 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.


    <string name="app_name">GfG | Button In Kotlin</string>
    <string name="btn">Button</string>
    <string name="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.  


<?xml version="1.0" encoding="utf-8"?>
    <!-- Button added in the activity -->
        app:layout_constraintTop_toTopOf="parent" />

Step 4: 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. 


import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    protected void onCreate( Bundle savedInstanceState ) {
        // storing ID of the button
        // in a variable
        Button button = (Button)findViewById(;
        // operations to be performed
        // when user tap on the button
        if (button != null) {
            button.setOnClickListener((View.OnClickListener)(new View.OnClickListener() {
                    public final void onClick(View it) {
                    // displaying a toast message
                    Toast.makeText((Context)MainActivity.this, R.string.message, Toast.LENGTH_LONG).show();


import android.os.Bundle
import android.widget.Button
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // storing ID of the button
        // in a variable
        val button = findViewById<Button>(
        // operations to be performed
        // when user tap on the button
            // displaying a toast message
            Toast.makeText(this@MainActivity, R.string.message, Toast.LENGTH_LONG).show() }



Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 17 Aug, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials