Button in Android

17 Aug, 2021
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.
XML Attributes
Description
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.
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.
XML
<resources>    <string name="app_name">GfG | Button In Kotlin</string>    <string name="btn">Button</string>    <string name="message">Hello Geeks!! This is a Button.</string></resources>
 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
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#168BC34A"    tools:context=".MainActivity">     <!-- Button added in the activity -->    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#4CAF50"        android:paddingStart="10dp"        android:paddingEnd="10dp"        android:text="@string/btn"        android:textColor="@android:color/background_light"        android:textSize="24sp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
 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. 
Java
Kotlin
import androidx.appcompat.app.AppCompatActivity;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 {     @Override    protected void onCreate( Bundle savedInstanceState ) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);         // storing ID of the button        // in a variable        Button button = (Button)findViewById(R.id.button);         // 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 androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.widget.Buttonimport android.widget.Toast class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)         // storing ID of the button        // in a variable        val button = findViewById<Button>(R.id.button)         // operations to be performed        // when user tap on the button        button?.setOnClickListener()        {            // displaying a toast message            Toast.makeText(this@MainActivity, R.string.message, Toast.LENGTH_LONG).show() }    }}
Output:
 
RISHU_MISHRA
anikaseth98
Android-Button
Kotlin Android
Picked
Android
Kotlin
Technical Scripter
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.