0

In my layout I added two edit text by click on the add button and its added(works) and when I click on delete button it also delete the dynamic edit text.My question is how to get string value from it suppose I dynamically add two views and also delete when not needed. here is my code..

public class PartDetails extends AppCompatActivity {

    // Parent view for all rows and the add button.
    private LinearLayout mContainerView;
    // The "Add new" imageButton
    private ImageButton mAddImageButton;
    Button submit_part_details;
    private View mExclusiveEmptyView;
    EditText prequired,pnumber;

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

        mContainerView = (LinearLayout) findViewById(R.id.parentView);
        submit_part_details = (Button) findViewById(R.id.submit_part_details);
        mAddImageButton = (ImageButton) findViewById(R.id.add_et_parts);
        prequired= (EditText) findViewById(R.id.et_Prequired);
        pnumber= (EditText) findViewById(R.id.et_Pnumber);
        mAddImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                inflateEditRow();
            }
        });
        submit_part_details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String parts=prequired.getText().toString();
                String pnum =pnumber.getText().toString();
            }
        });
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // TODO: Handle screen rotation:
        // encapsulate information in a parcelable object, and save it
        // into the state bundle.

    }
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // TODO: Handle screen rotation:
        // restore the saved items and inflate each one with inflateEditRow;

    }

    // Helper for inflating a row
    private void inflateEditRow() {

        LayoutInflater layoutInflater =
                (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View addView = layoutInflater.inflate(R.layout.row_add_parts, null);
        final EditText parts_required = (EditText) addView
                .findViewById(R.id.et_parts);
        final EditText parts_number = (EditText) addView
                .findViewById(R.id.et_Pnumber);
        final ImageButton deleteButton = (ImageButton) addView
                .findViewById(R.id.delete_et_parts);
        deleteButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ((LinearLayout) addView.getParent()).removeView(addView);
            }
        });

        mContainerView.addView(addView);
    }
}

this is parent XML layout

  <?xml version="1.0" encoding="utf-8"?>

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:fillViewport="true">

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:background="@android:color/black"
            android:layout_height="match_parent"
            android:focusableInTouchMode="true">


            <LinearLayout
                android:id="@+id/parentView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:animateLayoutChanges="true"
                android:layout_below="@+id/tv"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:orientation="vertical">

                <RelativeLayout
                    android:id="@+id/relative_layout1"
                    android:layout_marginTop="10dp"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/parts_required"
                        android:layout_width="110dp"
                        android:layout_height="40dp"
                        android:text="Parts Required"
                        android:textColor="#fff"
                        android:textSize="15sp"
                        android:textStyle="bold"
                        android:background="@drawable/shape"
                        android:gravity="center"/>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_toRightOf="@+id/parts_required"
                        android:layout_marginLeft="20dp"
                        android:orientation="horizontal">

                        <EditText
                            android:id="@+id/et_Prequired"
                            android:layout_width="0dp"
                            android:layout_height="40dp"
                            android:layout_weight="0.8"
                            android:hint="text"
                            android:singleLine="true"
                            android:inputType="text"
                            android:textSize="20sp"
                            android:paddingLeft="20dp"
                            android:imeOptions="actionNext"
                            android:background="@drawable/shape_edit_text"
                            />

                        <ImageButton
                            android:id="@+id/add_et_parts"
                            android:layout_width="0dp"
                            android:layout_height="40dp"
                            android:background="@null"
                            android:onClick="onAddNewClicked"
                            android:src="@android:drawable/ic_input_add"
                            android:layout_weight="0.2"/>


                    </LinearLayout>


                </RelativeLayout>
<RelativeLayout
                android:id="@+id/relative_layout2"
                android:layout_marginTop="5dp"
                android:layout_below="@+id/relative_layout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/parts_number"
                    android:layout_width="110dp"
                    android:layout_height="40dp"
                    android:text="Parts Number"
                    android:textColor="#fff"
                    android:textSize="15sp"
                    android:textStyle="bold"
                    android:background="@drawable/shape"
                    android:gravity="center"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_toRightOf="@+id/parts_number"
                    android:layout_marginLeft="20dp"
                    android:orientation="horizontal">

                    <EditText
                        android:id="@+id/et_Pnumber"
                        android:layout_width="0dp"
                        android:layout_height="40dp"
                        android:layout_weight="1"
                        android:hint="text"
                        android:singleLine="true"
                        android:inputType="text"
                        android:textSize="20sp"
                        android:paddingLeft="20dp"
                        android:imeOptions="actionNext"
                        android:background="@drawable/shape_edit_text"
                        />

                    <!-- <ImageButton
                         android:id="@+id/add_et_parts_number"
                         android:layout_width="0dp"
                         android:layout_height="50dp"
                         android:background="@null"
                         android:src="@android:drawable/ic_input_add"
                         android:layout_weight="0.2"/>-->

                </LinearLayout>

            </RelativeLayout>


                <Button
                    android:layout_marginTop="50dp"
                    android:id="@+id/submit_part_details"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_below="@+id/snap"
                    android:textAllCaps="false"
                    android:textSize="20sp"
                    android:textColor="#fff"
                    android:background="#1c4648"
                    android:text="Submit All Details" />

            </RelativeLayout>



        </RelativeLayout>
    </ScrollView> 

this is Add Button XML layout

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

    <RelativeLayout
        android:id="@+id/relative_layout1"
        android:layout_marginTop="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/parts_required"
            android:layout_width="110dp"
            android:layout_height="40dp"
            android:text="Parts Required"
            android:textColor="#fff"
            android:textSize="15sp"
            android:textStyle="bold"
            android:background="@drawable/shape"
            android:gravity="center"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/parts_required"
            android:layout_marginLeft="20dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/et_parts"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="0.8"
                android:singleLine="true"
                android:inputType="text"
                android:textSize="20sp"
                android:hint="text"
                android:paddingLeft="20dp"
                android:imeOptions="actionNext"
                android:background="@drawable/shape_edit_text"
                />

            <ImageButton
                android:id="@+id/delete_et_parts"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:background="@null"
                android:src="@android:drawable/ic_delete"
                android:layout_weight="0.2"/>


        </LinearLayout>


    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relative_layout2"
        android:layout_marginTop="5dp"
        android:layout_below="@+id/relative_layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/parts_number"
            android:layout_width="110dp"
            android:layout_height="40dp"
            android:text="Parts Number"
            android:textColor="#fff"
            android:textSize="15sp"
            android:textStyle="bold"
            android:background="@drawable/shape"
            android:gravity="center"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/parts_number"
            android:layout_marginLeft="20dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/et_Pnumber"
                android:layout_width="0dp"
                android:layout_height="40dp"
                android:layout_weight="1"
                android:hint="text"
                android:singleLine="true"
                android:inputType="text"
                android:textSize="20sp"
                android:paddingLeft="20dp"
                android:imeOptions="actionNext"
                android:background="@drawable/shape_edit_text"
                />

            <!-- <ImageButton
                 android:id="@+id/add_et_parts_number"
                 android:layout_width="0dp"
                 android:layout_height="50dp"
                 android:background="@null"
                 android:src="@android:drawable/ic_input_add"
                 android:layout_weight="0.2"/>-->

        </LinearLayout>

    </RelativeLayout>


</LinearLayout> 
vyas dipak
  • 109
  • 2
  • 15

2 Answers2

0

you can do it in many ways . here are two of them:

  1. make an ArrayList and whenever you add new edit text add it to this array too and when you delete remove it from here too. when you want to get their text put it in a for loop
  2. BETTER WAY : you have a linear layout. get its child count and put it in for loop. something like this :
    for (int i=0;i<mContainerView.getChildCount();i++){
        View mView=mContainerView.getChildAt(i);
        EditText myEditText=(EditText) mView.findViewById(R.id.et_parts);
        String txt=myEditText.getText().toString();
        EditText myEditText2=(EditText) mView.findViewById(R.id.et_Pnumber);
        String txt2=myEditText2.getText().toString();
    }
Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50
0
  ArrayList<EditText> edtParts_required=new ArrayList<EditText>();
  ArrayList<EditText> edtParts_number=new ArrayList<EditText>();

// Helper for inflating a row
private void inflateEditRow() {
    LayoutInflater layoutInflater =
            (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View addView = layoutInflater.inflate(R.layout.row_add_parts, null);
    final EditText parts_required = (EditText) addView
            .findViewById(R.id.et_parts);
    final EditText parts_number = (EditText) addView
            .findViewById(R.id.et_Pnumber);

    //Add EditText object to Collection 
     edtParts_required.add(parts_required);
     edtParts_number.add(parts_required);

    final ImageButton deleteButton = (ImageButton) addView
            .findViewById(R.id.delete_et_parts);
    deleteButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ((LinearLayout) addView.getParent()).removeView(addView);
        }
    });

    mContainerView.addView(addView);
}

When you delete the inflated row remove the element from collection too 

Loop through your Collection and get the text from dynamic EditText
for(int i=0;i<edtParts_required.size();i++){
   String enterdText=edtParts_required.get(i).getText().toString();
}
Raghavendra B
  • 441
  • 3
  • 10