Dynamic Button in Kotlin - GeeksforGeeks

11 Sep, 2021
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:
XML
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout        xmlns:android="http://schemas.android.com/apk/res/android"        xmlns:tools="http://schemas.android.com/tools"        xmlns:app="http://schemas.android.com/apk/res-auto"        android:layout_width="match_parent"        android:layout_height="match_parent"        tools:context=".MainActivity">     <LinearLayout            android:id="@+id/layout"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:gravity="center"            android:orientation="vertical">    </LinearLayout> </android.support.constraint.ConstraintLayout>
After setting up the design our final step is to code our progress bar. Open app/src/main/java/yourPackageName/MainActivity.kt 
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,
 ViewGroup.LayoutParams.WRAP_CONTENT)
 
then, add the button in the layout using addView() 
       
 val layout = findViewById(R.id.layout) as LinearLayout
 // add Button to LinearLayout
        layout.addView(button)
Java
package com.geeksforgeeks.myfirstkotlinapp import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.ViewGroupimport android.widget.Buttonimport android.widget.LinearLayoutimport android.widget.Toast  class MainActivity : AppCompatActivity() {      override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)          val layout = findViewById(R.id.layout) 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        layout.addView(button)    }}
XML
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="i.apps.button">      <application            android:allowBackup="true"            android:icon="@mipmap/ic_launcher"            android:label="@string/app_name"            android:roundIcon="@mipmap/ic_launcher_round"            android:supportsRtl="true"            android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                  <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>    </application>  </manifest>
saurabh1990aror
anikakapoor
Picked
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Broadcast Receiver in Android With Example
Content Providers in Android with Example
Android RecyclerView in Kotlin
Android UI Layouts
Retrofit with Kotlin Coroutine in Android
Android Menus
MVP (Model View Presenter) Architecture Pattern in Android with Example
Kotlin Setters and Getters
MVC (Model View Controller) Architecture Pattern in Android with Example
Kotlin Coroutines on Android