How to create a dynamic array button in Android?

Related searches

I have a button. I want to write the function "onClick" for the button. Each time that I click on it I want a new button to be created on screen. And also that new button to be added into an array list of buttons.

I searched but I couldn't find any answer. All of the answers talk about creating buttons with drop and drag, not with dynamic creation of them.

Thanks

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to add"
        android:id="@+id/B1"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:onClick="clicktoaddcode"
        />
</RelativeLayout>

package com.example.yas.dynamicbutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    public void clicktoaddcode(View V){
        Toast.makeText(MainActivity.this, "Created", Toast.LENGTH_SHORT).show();
    }
}

You must using ArrayList.

List<Button> btnList = new ArrayList<>();

// Call Your ClickListener
btnList.add(new Button(YourContext));
YourParent.addView(btnList.get(btnList.size() - 1 ));

Edited

MainActivity.java

public class MainActivity extends AppCompatActivity {

    LinearLayout container;

    List<Button> btnList = new ArrayList<>();

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

        container = (LinearLayout)findViewById(R.id.container);


    }

    public void addBtn(View v){
        btnList.add(new Button(this));
        container.addView(btnList.get(btnList.size() - 1 ));
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:clickable="true"
        android:onClick="addBtn">

    </LinearLayout>

</android.support.design.widget.CoordinatorLayout>

How To Generate Dynamic Button in android, You must using ArrayList. List<Button> btnList = new ArrayList<>(); // Call Your ClickListener btnList.add(new Button(YourContext)); YourParent. This example demonstrates how do I add a button dynamically in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.


Have this for your XML -

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <Button
            android:id="@+id/myButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Add one more" />

        <LinearLayout
            android:orientation="vertical"
            android:id="@+id/myLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center">

        </LinearLayout>

    </LinearLayout>
</ScrollView>

And this in Java-

final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.myLinearLayout);
    Button button = (Button)findViewById(R.id.myButton);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Button button = new Button(MainActivity.this);
            button.setText("A new Button");
            linearLayout.addView(button);
        }
    });

Button Clicks using Array and For Loop (Part 2) Android Studio , In android Generate dynamic button and also handle this all button click event using Onclik Duration: 12:02 Posted: May 13, 2017 I had a similar problem trying to create a dynamic array of checkboxes, so the number of array elements is undetermined until runtime. In order to solve this, I have to have a loop to determine the number of array elements. Then I had to allocate the array as follows: b1 = new Button[arrayCount];


The most simple method to create an unlimited size array is using String Builder. This allows you to add many elements in runtime. It has 3 major components.

  1. Declaration and Initialization.
  2. Value addition/ Append
  3. Print out/ Usage

Use the bellow code anywhere in Android Studio.

//Basic Decleration
        StringBuilder myString = new StringBuilder();
        
//Adding data
       myString.append("Hello text <br>");
        myString.append("some more data <br>");
        myString.append("more data to store <br>");
        
//Displaying in already created text view
        deviceDetails.setText(Html.fromHtml(callLogs+""));

Dynamically Create View Elements, How to Add Button Clicks on multiple buttons in Android Studio using an Array and a For Loop.Duration: 29:09 Posted: Sep 12, 2017 In android Generate dynamic button and also handle this all button click event using Onclik listener interface and set tag() ,getTag() methods.


Dynamically add elements in ListView in android?, Creating textview , buttons dynamically and creating onclick listener for buttons. After click on button show index(id). File : src/GlobalClass.java� In android you can Generate dynamic button and also handle all button click event you want to use.


This example demonstrates how do I dynamically add elements in MainActivity " > <Button android:id="@+id/button1" android:layout_width="fill_parent" asList (ListElements)); final ArrayAdapter<String> adapter = new� Android create Button programmatically. In this tutorial i am making a button widget through MainActivity.java programming file. Creating button through this procedure are called as dynamic button. So here is the complete step by step tutorial for Create Button dynamically through MainActivity in android.


In Android sometimes you need to create layouts programatically or dynamically according to your need. So today’s is about how to create layouts programatically in android. Below is a method that I am going to use in this article. In this i am creating 5 Buttons dynamically and setting their ID’s for further use. After