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 ;)