Open In App

How to Generate Dynamic Multiple Buttons in Android?

Last Updated : 23 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Android Studio, buttons are graphical user interface (GUI) elements that users can click or tap to perform an action. Buttons are typically represented by a rectangular or rounded rectangular shape with a label or an icon. In this article, we will learn to make dynamic multiple buttons in android studio. By which we can make unlimited buttons in the layout. A sample video is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Java language.

Step by Step Implementation

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Here is the build.gradle (app) file

XML




plugins {
    id 'com.android.application'
}
  
android {
    namespace 'com.example.dynamicmultiplebuttonsgfg'
    compileSdk 33
  
    defaultConfig {
        applicationId "com.example.dynamicmultiplebuttonsgfg"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"
  
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
  
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
  
dependencies {
  
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}


Step 3: Make a design to show buttons user interface

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@color/white">
  
    <LinearLayout
        android:id="@+id/linear_Layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
  
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me for clone"
        android:backgroundTint="#2ECC35"/>
  
    </LinearLayout>
  
</RelativeLayout>


The above code looks like the below image in the user interface:

UI

Step 4: Working with the MainActivity.java file

Go to the app > res > layout > MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.

Java




package com.example.dynamicmultiplebuttonsgfg;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    Button btn;
    Button newBtn;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        btn = findViewById(R.id.button);
  
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addButton();
            }
        });
    }
  
    private void addButton() {
        LinearLayout layout = findViewById(R.id.linear_Layout);
        newBtn = new Button(this);
        newBtn.setText("New Button");
        layout.addView(newBtn);
    }
}


Now all are set, just run your app and you will be able to see the output of dynamic multiple buttons.

Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads