0

I want to create EditTextFields dynamically on depending the condition. Condition is that if I start typing on first EditTextField it will create one more EditTextField in the bottom and will create the third EditTextField when i start typing on second one. Similarly i want to delete the bottom text if there is no text in the upper EditTextField. Thanks.

  • 2
    Are you only going to have 3 editText fields? If so I would create them in the view and show / hide if text is in other editText field. Do you have some code? What have you tried? – jasonflaherty Sep 27 '13 at 15:16
  • I agree that the show/hide method is probably the easiest if you have a concrete number of max fields known before hand. – nhgrif Sep 27 '13 at 15:20
  • No not 3 editText fields. Dynamically we should add any number of editText fields. –  Sep 27 '13 at 15:44

3 Answers3

2

Use a parent view, like a ScrollView that you know you can add a flexible about of content to. Then use a TextWatcher a/k/a a text change listener. You could then create a new text view which you would add to the ScrollView if text was typed into the EditText field.

For neatness I'd probably create a custom TextView class that housed this text change listener and replication check. Here's example of how you could add a TextView

//instance variable
private LinearLayout containerLayout;
private newTextViewCreated = false;
//initialize your conatinerLayout before you use it
//and create your first edit text field
public void onCreate(Bundle savedInstaceState){
   containerLayout = (LinearLayout)findViewById(R.id.conatinerLinearLayout);
   createEditText();
}

private void createEditText(){
    EditText editText = new editText(this);
    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(count > 0 && !newTextViewCreated){
                createEditText();
                newTextViewCreated = true;
            }           
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub              
        }

        @Override
        public void afterTextChanged(Editable s) {
            //TODO Auto-generated method stub  
        }
    });
    containerLayout.addView(editText);
}

I didn't test this out, I'm writing it now but here's what I'm thinking. Read the description of how a TextWatcher works so you understand the inner methods. You're going to have to play with the conditionals but what you're doing is listening for a change in the number of characters entered and then making a recursive call to create an additional view when chars are added to each text view. I use a boolean flag to show when a view has been created so we don't add one each time the char is changed. I moved outside the createEditText method based on your comment. If you made your own EditText class you could just add a method that would set/get the status of whether this TextView had spanwed another. To remove you would just add a delete condition that would remove the view from the linear layout.

Community
  • 1
  • 1
Rarw
  • 7,645
  • 3
  • 28
  • 46
  • But how to add and remove the EditText fields dynamically. –  Sep 27 '13 at 21:53
  • You're going to use the `TextWatcher`. It will tell you everything you need to know about what's in that EditText. I'll add an example above. – Rarw Sep 27 '13 at 22:31
  • It is not possible to use an non-final variable inside an inner class. –  Sep 30 '13 at 15:09
  • That's true - that's what I get for responding from my phone. The example is more for illustrative purposes. It's not an exact implementation. I'd still use a flag of some sort so that I don't create a text view each time a char is added to the field. Could just create a class that extends EditText and has its own instance variable to track whether a new TextView has been spawned. That you could set from the TextWatcher on that view. – Rarw Sep 30 '13 at 16:28
0

Show / Hide them in your layout if you know the total amount of editText fields needed or add them programatically like so:

EditText myET = new EditText(MyActivity.this);
myET.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
LayoutContentView.addView(myET);

Then check:

if (myET.getText().toString().trim().equals(""))
{
  //Don't Show
}else{
   //SHOW
}

SO question could help:https://stackoverflow.com/a/6792359/350421

EditText toAdd = new EditText(this);
list.add(toAdd);
Community
  • 1
  • 1
jasonflaherty
  • 1,924
  • 7
  • 40
  • 82
0

User TextWatcher Implement your Activity with TextWatcher and override method

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
Muhammad Aamir Ali
  • 20,419
  • 10
  • 66
  • 57
  • Yeah I tried that in onTextChanged. But how to add and remove the EditText fields dynamically. –  Sep 27 '13 at 15:48