Create An Android Button Programmatically in Kotlin

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

Hello Readers! In this post, we will learn about how to create Android Button programmatically in kotlin.

You may also visit post to know more about Button and it’s useful xml attributes to customise it.

Output

Tutorialwing - Android Button programmatically in Kotlin Create button dynamically in kotlin Create Android Button Widget

Tutorialwing – Android Button programmatically in Kotlin

Video Output

At first, we will create an android application in kotlin. Then, we will create button and use it in the application.

1. Creating New Project

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as DynamicButton. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully.

You may also visit our post to know detailed steps to create android project. This tutorial is covered in java. However, the steps are similar in kotlin too.

Now, we will modify the xml and kotlin file to create and use Button programmatically in kotlin.

2. Modify Values Folder

Open res/values/strings.xml file and add below code into it.

<resources>
	<string name="app_name">DynamicButtonUsingKotlin</string>
	<string name="show_text">Show Text</string>
	<string name="welcome_message">Welcome to Button Tutorial Using Kotlin!</string>
</resources>

Other values folder have not been changed. So, we are not going to mention it here.

3. Modify Layout Folder

Open res/layout/activity_main.xml file and add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	android:id="@+id/rootContainer"
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:gravity="center"
	android:orientation="vertical">

</LinearLayout>

Here, LinearLayout will act as container for the Button created dynamically in kotlin file.

4. Create Android Button Programmatically / Dynamically in Kotlin

Open app/src/main/java/com.tutorialwing.dynamicebutton/MainActivity.kt file and add below code into it. You may also visit post to Use Button in xml file in kotlin.

package com.tutorialwing.dynamicbutton

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val linearLayout = findViewById<LinearLayout>(R.id.rootContainer)

        // Create Button Dynamically
        val btnShow = Button(this)
        btnShow.setText(R.string.show_text)
        btnShow.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        btnShow.setOnClickListener { Toast.makeText(this@MainActivity, R.string.welcome_message, Toast.LENGTH_LONG).show() }

        // Add Button to LinearLayout
        linearLayout?.addView(btnShow)
    }
}

Here, we are creating Button programmatically in kotlin file. Then, adding it into LinearLayout having id rootContainer.

Since AndroidManifest file is very important file in android application. We are also going to mention it here.

AndroidManifest.xml file

Code inside src/main/AndroidManifest.xml file would look like below –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.dynamicbutton"
		  xmlns:android="http://schemas.android.com/apk/res/android">

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

Now, run the application. We will get output as shown above.

That’s end of tutorial on Creating Android Button programmatically in kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners