1

I am having a layout with a single editText in it,below the editText I added a add button. Now my question is when I click the add button I need to get another editText below the editText and has to repeat the same.Please anyone help, Thank you.

2 Answers 2

1

activity_main.xml

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

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

        <EditText
            android:id="@+id/et1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <Button
        android:id="@+id/buttonAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD" />
</LinearLayout>

and put following code in your Activity

final LinearLayout llEditTextsContainer = (LinearLayout) view.findViewById(R.id.ll_edit_texts_container);
        Button buttonAdd = (Button) view.findViewById(R.id.buttonAdd);
        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText editText = new EditText(getActivity());
                //editText.setId(); you should set id smartly if you wanted to use data from this edittext
                llEditTextsContainer.addView(editText);
            }
        });
Sign up to request clarification or add additional context in comments.

1 Comment

@Ajay kumar did my answer help you?
1

First of all create a container view in your main layout which is going to hold the new Edittexts.

<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.vuclip.dynamictextedit.MainActivity">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/addTextView"
    android:text="Add EditText"
    android:onClick="onClick"
    />

<TableLayout
    android:id="@+id/containerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</TableLayout>

Here, on pressing the button, new Edittexts will be created and added into the TableLayout.

Now create a new Layout which will hold your edittext(I called this file new_layout.xml).

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
    android:id="@+id/newEditText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

Now add this layout into your main activity.

public class MainActivity extends AppCompatActivity {

TableLayout container;
static int rowIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    container = (TableLayout) findViewById(R.id.containerLayout);
}

public void onClick(View view) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View newRowView = inflater.inflate(R.layout.new_layout,null);
    container.addView(newRowView,rowIndex);
    rowIndex++;
}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.