Q:

How to Create a Checkbox Automatically on user input

In My Activity i have a Edit Text and Button When the user enter the button " Add " the checklist want to create Automatically with the name of the user entered in the edit text
how to create a checkbox in java program based on the user input

when i  click the Add Button the list was not updated ( the userinput was not converted into checkbox and it doesnot display the name in bottom )
Here is My XML Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">

  <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:hint="Enter Name to Add in the list"
      android:id="@+id/ed_name"
      android:layout_marginTop="15dp"
      />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/btn_add"
        android:layout_below="@+id/ed_name"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        />
    

</RelativeLayout>

here is My java code
public class MainActivity extends AppCompatActivity {

EditText uinput;
Button btnadd;
CheckBox cbname;
ScrollView sv;
RelativeLayout ll;

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

    sv = new ScrollView(this);
    ll = new RelativeLayout(this);
   // ll.setOrientation(LinearLayout.VERTICAL);
    sv.addView(ll);

    uinput = findViewById(R.id.ed_name);
    btnadd = findViewById(R.id.btn_add);
    btnadd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String Name = uinput.getText().toString();
            CheckBox cb = new CheckBox(getApplicationContext());
            cb.setText(Name);
            ll.addView(cb);

        }
    });
}

A:

I suggest you to add everything that is static in the activity_main.xml layout file as :
    <?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=".MainActivity2">

    <EditText
        android:id="@+id/ed_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:hint="Enter name to add in the list"
        android:inputType="textPersonName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="Add"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ed_name" />

    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="409dp"
        android:layout_height="612dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_add">

        <LinearLayout
            android:id="@+id/linear_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

Now, Checkbox are the one which we will generate programmatically and add them to the LinearLayout which is the only child of ScrollView(it supports single child only). A sample code for MainActivity doing that is here :
    public class MainActivity extends AppCompatActivity {

    // declare the required views variable
    private EditText uInput;
    private LinearLayout linearLayout;

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

        // initializes required view variable
        uInput = findViewById(R.id.ed_name);

        linearLayout = findViewById(R.id.linear_layout);

        Button btnAdd = findViewById(R.id.btn_add);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = uInput.getText().toString();
                if (!name.isEmpty()) {
                    CheckBox checkBox = new CheckBox(MainActivity.this);
                    checkBox.setText(name);
                    linearLayout.addView(checkBox);
                } else
                    Toast.makeText(MainActivity.this, "The name cannot be empty!", Toast.LENGTH_LONG).show();
            }
        });

    }
}

It also checks whether or not the text in EditText is empty and if it is generates a suitable Toast message to not permit the user create a empty Checkbox. Hope, this helps!

