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.

Output

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.

<resources>
	<string name="app_name">DynamicButton</string>
	<string name="show_text">Show Text</string>
	<string name="welcome_message">Welcome to Button Tutorial!</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, 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/MainActivity.java 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.support.v7.app.AppCompatActivity;
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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		LinearLayout linearLayout = findViewById(R.id.rootContainer);

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

		// Add Button to LinearLayout
		if (linearLayout != null) {
			linearLayout.addView(btnShow);
		}
	}
}

In MainActivity.java 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"
		  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 the output as shown above.

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

Leave a Reply