'How to programmatically add different buttons to a table, android studio

I'm new to android application development, I need to add buttons with different names, that is, the name of the button is taken from EditText and a button is created. The nuance of all this is that after I try to create another button and enter another name, this very name is transferred to other buttons. That is, if I create a button for the first time, then it is given the text Sometning1, but after I create the second button with the text Sometning2, then the first button gets the name of the second one, and so on, all previous buttons get the name of the new one, how to fix this? By the way, each button should be completely different, with different properties, but for now let's focus on the text in it.(sorry, hard to explain) Xml code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
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"
tools:context=".MainActivity6">
<TableLayout
    android:id="@+id/Tb1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:ignore="MissingConstraints">

    <TableRow
        android:id="@+id/r1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <TableRow
        android:id="@+id/r2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TableRow>

    <TableRow
        android:id="@+id/r4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TableRow>
    <TableRow
        android:id="@+id/r5"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TableRow>
    <TableRow
        android:id="@+id/r6"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TableRow>
    <TableRow
        android:id="@+id/r7"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TableRow>
    <TableRow
        android:id="@+id/r8"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></TableRow>


</TableLayout>

<EditText
    android:id="@+id/adress"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="70dp"
    android:inputType="text"
    android:text="Something"
    android:textColor="#424242"
    app:layout_constraintBottom_toBottomOf="@+id/btn1"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:ignore="LabelFor" />

<Button
    android:id="@+id/btn1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="40dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

 package com.example.serverconnectionukaa;

 import androidx.appcompat.app.AppCompatActivity;

 import android.content.Context;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TableLayout;
 import android.widget.TableRow;
 import android.widget.Toast;

public class MainActivity6 extends AppCompatActivity {
EditText adress;
Button btn1;
private TableLayout buttonTableLayout;
int clickcount=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main6);
    adress = findViewById(R.id.adress);
    btn1 = (Button) findViewById(R.id.btn1);
    buttonTableLayout = (TableLayout) findViewById(R.id.Tb1);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clickcount=clickcount+1;
            if(clickcount==0)
            {

            }
            else
            {
                //check how many times clicked and so on
                Toast.makeText(getApplicationContext(),"Button clicked count is"+clickcount, Toast.LENGTH_LONG).show();
            }
            for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
                ((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            int x = 0;
                for (int row = 0; row < 5; row++) {
                    TableRow currentTableRow = getTableRow(row);

                    for (int column = 0; column < 2; column++) {

                        Button newGuessButton = new Button(MainActivity6.this);
                        
                        String myName = adress.getText().toString();

                        newGuessButton.setText(myName);
                        currentTableRow.addView(newGuessButton);
                        x++;
                        if (x==clickcount) {
                           break;
                        }
                    }
                    if (x==clickcount) {
                        break;
                    }
             }
        }
    });
}

private TableRow getTableRow(int row) {
    return (TableRow) buttonTableLayout.getChildAt(row);
}
}

Thanks to everyone



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source