3

I am making an app where at a certain fragment, I need a variable number of EditTexts in the fragment. I have an add button below the layout which when pressed should add the required Edit Text with ID's so that I can collect data from it.

For eg, if the layout starts out with Initial Layout

When I press the + button, it should add like Second Layout

So as I keep pressing the + button, I should automatically get one more layout with all the edit texts. And I need a way to keep track of the ids of all the edit texts so that I can get all the data later.

How do I go about doing this???

Sriram R
  • 2,109
  • 3
  • 23
  • 40
  • What have you tried so far? Show us some code – Mehmet K Sep 18 '17 at 12:09
  • I havent tried anything yet. I thought of a lot of ways but I could'nt figure out a way to do it @Mehmet – Sriram R Sep 18 '17 at 12:11
  • for adding elements dynamically you can get some help from [here](http://saigeethamn.blogspot.in/2010/12/android-ui-inflate-from-xml-dynamic-ui.html). and to provide the ID's you can some helpful code from [here](https://stackoverflow.com/a/8937477/7073808) – UltimateDevil Sep 18 '17 at 12:14
  • Look into using a [ListView](https://developer.android.com/guide/topics/ui/layout/listview.html) or [RecyclerView](https://developer.android.com/guide/topics/ui/layout/recyclerview.html). – Mehmet K Sep 18 '17 at 19:10

2 Answers2

1

Design your layout of EditText's in xml as a my_item.xml file :

<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/et1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

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

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

In your fragment add a LinearLayout to add dynamic items in it and a Button like this:

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


<Button
    android:id="@+id/btn_add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="+" />

Now in java code we inflate the my_item layout and add it to ll_dynamicItems. We also need a List of LinearLayout's to store inflated layout's in it:

List<LinearLayout> myLayouts = new ArrayList<>();

btn_add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        LinearLayout ll = (LinearLayout) getLayoutInflater().from(getApplicationContext()).inflate(R.layout.my_item, ll_dynamicItems, false);
        myLayouts.add(ll);
        ll_dynamicItems.addView(ll);
    }
});

Now for get a first layout first EditText value, you can do like this:

((EditText) myLayouts.get(0).findViewById(R.id.et1)).getText()

For get a second layout third EditText:

((EditText) myLayouts.get(1).findViewById(R.id.et3)).getText()

For reading all EditText's value you can track the list with a for ;)

SiSa
  • 2,594
  • 1
  • 15
  • 33
0

Actually you can take linearlayout vertical orientation and add edittexts whenever user presses plus button,programatically you can add to layout. you maintain some random numbers you add it

       EditText ed = new EditText(this);

        ed.setId(1);
        ed.setText("" + i);

        ed.setInputType(2);

        ed.setLayoutParams(lparams);

        textFieldsLayout.addView(ed)
skyshine
  • 2,767
  • 7
  • 44
  • 84