Open In App
Related Articles

Dynamic Button in Kotlin

Improve Article
Save Article
Like Article

In Android, a button represent something that can be clicked by the user to perform some action.
Firstly, we need to create a project in Android Studio. To do follow these steps:

Click on File, then New, and then New Project, and give name whatever you like: 

  • Click on File, then New, and then New Project, and give name whatever you like
  • Then, select Kotlin language Support and click next button.
  • Select minimum SDK, whatever you need.
  • Select Empty activity and then click finish.

After doing this you will see some directories on the left hand side after your project/gradle is finished loading. It should look like this: 

Second step is to design our layout page. Go to app > res > layout and paste the following code:

Modify activity_main.xml file


<?xml version="1.0" encoding="utf-8"?>

After setting up the design our final step is to code our progress bar. Open app/src/main/java/yourPackageName/MainActivity.kt 

Create Button in MainActivity.kt file

First of all, we define the button and set its attributes.  

val button = Button(this)
        // setting layout_width and layout_height using layout parameters
        button.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,

then, add the button in the layout using addView() 

 val layout = findViewById( as LinearLayout
 // add Button to LinearLayout


package com.geeksforgeeks.myfirstkotlinapp
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        val layout = findViewById( as LinearLayout
        // creating the button
        val button = Button(this)
        // setting layout_width and layout_height using layout parameters
        button.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        button.text = "WELCOME TO GFG"
        button.setOnClickListener { Toast.makeText(this@MainActivity, "Hello GEEK", Toast.LENGTH_LONG).show() }
        // add Button to LinearLayout

AndroidManifest.xml file


<?xml version="1.0" encoding="utf-8"?>
        <activity android:name=".MainActivity">
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>

Run as Emulator:

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 : 11 Sep, 2021
Like Article
Save Article
Similar Reads
Complete Tutorials