-1

This is what my XML currently looks like:

<?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">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Typ hier in welk cijfer u wilt komen te staan:"
    android:textSize="30sp"
    android:gravity="center"
    android:paddingTop="10dp"
    android:textStyle="bold"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/EditText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Vul hier het cijfer in"
        android:paddingTop="100dp"
        android:background="@null"
        android:paddingLeft="30dp"
        android:maxLength="4"
        android:inputType="numberDecimal"/>

    <Button
        android:id="@+id/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Volgende"
        android:layout_marginLeft="75dp"/>
</LinearLayout>

I want to add lets say 10 more EditText input fields in the interface of my app when the button is clicked. Could anyone point out to me how to do this? Thanks.

Pete Suza
  • 9
  • 3
  • 2
    Hello. Have a look at this : https://stackoverflow.com/questions/2395769/how-to-programmatically-add-views-to-views – Bruno Aug 13 '18 at 16:01
  • 4
    Possible duplicate of [How to Programmatically Add Views to Views](https://stackoverflow.com/questions/2395769/how-to-programmatically-add-views-to-views) – ivcubr Aug 13 '18 at 16:23

1 Answers1

1

Try this

LinearLayout mLayout = (LinearLayout) findViewById(R.id.linearLayout);
Button mButton = (Button) findViewById(R.id.my_button);
mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText editText = new EditText(TmyContext);
            editText.setLayoutParams(new 
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.WRAP_CONTENT));
            mLayout.addView(editText);
        }
    });
Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53