Create An Android Button Programmatically In Android

Hello Reader! In this post, we are going to learn about creating android button programmatically in android. We will go through different steps that explains how to create and use android button programmatically in any android application.

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


Tutorialwing Android Dynamic Button Create Button programmatically in android create button dynamically in android

Android Dynamic Button Output

Video Output

1. Creating New Project

At First, we will create android application in android. Then, we will use button dynamically. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as DynamicButton. Then, 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. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify java and xml file to use button programmatically.

2. Modify Values Folder

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

	<string name="app_name">DynamicButton</string>
	<string name="show_text">Show Text</string>
	<string name="welcome_message">Welcome to Button Tutorial!</string>

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


Here, We have only LinearLayout that acts as container for the Button created in java file at run time. Now, we will add code to create Button programmatically in java file.

4. Create Android Button Programmatically / Dynamically

Open app/src/main/java/com.tutorialwing.dynamicebutton/ file and add below code into it. You may also visit post to use Button widget in xml file in android

package com.tutorialwing.dynamicbutton;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

	protected void onCreate(Bundle savedInstanceState) {

		LinearLayout linearLayout = findViewById(;

		// Create Button Dynamically
		Button btnShow = new Button(this);
		btnShow.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
		btnShow.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Toast.makeText(MainActivity.this, R.string.welcome_message, Toast.LENGTH_LONG).show();

		// Add Button to LinearLayout
		if (linearLayout != null) {

In file, we have created button programmatically. Then, we have added it in LinearLayout having id rootContainer.
Due to Importance of Manifest file in any project, we are also going to see the code inside this file.

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"

		<activity android:name=".MainActivity">
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>


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

That’s end of Tutorial on Creating Android Button programmatically in java project.

Leave a Reply