-1

I have added 3 EditTexts and one Button using layout inflater

Below is my java code:

mAddButton.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
                    if (!et_boiler_make_boiler_row.getText().toString().equalsIgnoreCase("")) {
                        onAddNewClicked();
                        buttonRemove.setVisibility(View.VISIBLE);
                    } else {
                        Toast.makeText(ConsumerCompanyProfileDetails4.this, "Can not add more rows. :(",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });
   private void onAddNewClicked() {
                LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View addView = layoutInflater.inflate(R.layout.boiler_row, null);

                buttonRemove = (ImageView) addView.findViewById(R.id.remove);
                et_boiler_make_boiler_row = (EditText) addView.findViewById(R.id.et_boiler_make_boiler_row);
                et_capacity_boiler_row = (EditText) addView.findViewById(R.id.et_capacity_boiler_row);
                et_estd_year_boiler_row = (EditText) addView.findViewById(R.id.et_estd_year_boiler_row);

                buttonRemove.setOnClickListener(new View.OnClickListener() {

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

                mContainerView.addView(addView);
            }

Now i want to check whether all the 3 EditTexts have some value or not before adding a new row. i.e. I want to add validations for EditTexts which is added dynamically before adding a new row.

And also i want to know how to get the values from added EditTexts.

Please help !

Apoorv Mehrotra
  • 607
  • 5
  • 13
Android
  • 133
  • 3
  • 13

4 Answers4

0

Save the edittext views in a List. Check the list before adding new edittext view with for-each and get the text of each edittext with edittext.getText().toString(). Now you can check isEmpty() or whatever you want.

beeb
  • 1,615
  • 1
  • 12
  • 24
0

You can iterate over mContainerView childrens

for (int i = 0; i < mContainerView.getChildCount(); i++) { // check all rows
       View v = mContainerView.getChildAt(i);
       EditText editText = (EditText)view.findViewById(R.id.et_capacity_boiler_row);
        //do whatever you want
}

or just select last index

View v = mContainerView.getChildAt(mContainerView.getChildCount()-1);
EditText editText = (EditText)view.findViewById(R.id.et_capacity_boiler_row);
//do whatever you want

(assuming mContainerView contains only added views)

  • Yes... it worked... I have used a flag to check wheather edittext has text or not.. your solution is perfect.. thanks.. – Android Feb 06 '17 at 05:43
0

There is lots of way to do that, Please check my below solution.

While adding new row, add tag(some value should be unique for all ) for all edittext. For exe ,

et_boiler_make_boiler_row = (EditText) addView.findViewById(R.id.et_boiler_make_boiler_row);
et_boiler_make_boiler_row.setTag(1);
et_capacity_boiler_row = (EditText) addView.findViewById(R.id.et_capacity_boiler_row);
et_capacity_boiler_row.setTag(1);
et_estd_year_boiler_row = (EditText) addView.findViewById(R.id.et_estd_year_boiler_row);
et_estd_year_boiler_row.setTag(1);

and check tag wherever required and do your work.

samsad
  • 1,241
  • 1
  • 10
  • 15
0

Please Try this... I think It will help you.

String[] str = {"AAA","BBB","CCC"};
mAddButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if (!str[0].equalsIgnoreCase("") && !str[1].equalsIgnoreCase("") && !str[2].equalsIgnoreCase("")) {
            onAddNewClicked();
            str = {"","",""};
        } else {
            Toast.makeText(ConsumerCompanyProfileDetails4.this, "Can not add more rows. :(",
                        Toast.LENGTH_SHORT).show();
        }
    }
});

and method like this for check black value available or not.

private void onAddNewClicked() {
        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View addView = layoutInflater.inflate(R.layout.boiler_row, null);

        ImageView buttonRemove = (ImageView) addView.findViewById(R.id.remove);
        EditText et_boiler_make_boiler_row = (EditText) addView.findViewById(R.id.et_boiler_make_boiler_row);
        EditText et_capacity_boiler_row = (EditText) addView.findViewById(R.id.et_capacity_boiler_row);
        EditText et_estd_year_boiler_row = (EditText) addView.findViewById(R.id.et_estd_year_boiler_row);

        et_boiler_make_boiler_row.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                str[0] = s;
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        et_capacity_boiler_row.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                str[1] = s;
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        et_estd_year_boiler_row.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                str[2] = s;
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        buttonRemove.setOnClickListener(new View.OnClickListener() {

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

        mContainerView.addView(addView);
}

in this solution you will check all three EditText values enter by user or not. If user enter something and than erase it than also it gives you the blank value. So, It helps you.. I hope this solution will be fullfill your requirements.

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54