Search code examples
androidandroid-edittexttextchanged

create N number of Edittext on button click


I want to create Edittext one below one on a button click.So i tried the following code.

    private LinearLayout mLayout;
    private EditText mEditText;
    private Button mButton;
    int k = -1;
    int flag;
    int ss=0;
    ArrayList<String> applnserverinstnos = new ArrayList<String>();
    EditText textView[] = new EditText[100];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLayout = (LinearLayout) findViewById(R.id.linearLayout);
        mEditText = (EditText) findViewById(R.id.editText);
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                k++;

                flag=k;
                final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, 50);
                lparams.setMargins(20, 20, 20, 0);
                textView[flag] = new EditText(AddEditBoxActivity.this);
                textView[flag].setLayoutParams(lparams);
                textView[flag].setId(flag);
                applnserverinstnos.add(flag,Integer.toString(flag));
                textView[flag].addTextChangedListener(new TextWatcher() {

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

                        }

                        @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
                             String c = s.toString(); // read Content
                             applnserverinstnos.add(flag,c);
                             System.out.println("insno       "+applnserverinstnos.get(flag));
                        }
                    });

                mLayout.addView(textView[flag]);


            }
        });
        TextView textView = new TextView(this);
        Button b2= (Button)findViewById(R.id.button1);
        b2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                for(int i = 0;i<=k;i++) 
                {
                System.out.println("applserver   "+i+".."+applnserverinstnos.get(i));
                }
            }
        });

    }

This creates the edittext one below one.I tried creating 3 edittext.But when i try to enter texts into edit text it throws following error.

06-13 12:44:47.686: E/AndroidRuntime(1665): java.lang.IndexOutOfBoundsException: Invalid index 3, size is 0

Please help me to find it. Thank You.

EDIT

after adding applnserverinstnos.add(flag,Integer.toString(flag)); it is not throwing error but the text changed in edittext one is not storing in position one instead it is storing in last position


Solution

  • Finally i found solution which is very simple and small.Now the following code creates N number of edittexts and also get the value from those edittexts created.

     private LinearLayout mLayout;
        private EditText mEditText;
        private Button mButton;
        int k = -1;
        int flag;
        int ss=0;
        ArrayList<String> applnserverinstnos = new ArrayList<String>();
        public static EditText textView[] = new EditText[100];
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mLayout = (LinearLayout) findViewById(R.id.linearLayout);
            mEditText = (EditText) findViewById(R.id.editText);
            mButton = (Button) findViewById(R.id.button);
            mButton.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    try
                    {
                    k++;
                    flag=k;
                    final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, 50);
                    lparams.setMargins(20, 20, 20, 0);
                    textView[flag] = new EditText(AddEditBoxActivity.this);
                    textView[flag].setLayoutParams(lparams);
                    textView[flag].setId(flag);
    
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                    mLayout.addView(textView[flag]);
    
    
                }
            });
    
            Button b2= (Button)findViewById(R.id.button1);
            b2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    for(int i = 0;i<=k;i++) 
                    {
                    System.out.println("edit values"+ss+"......"+ textView[i].getText().toString());
                    }
                }
            });
    
        }
    

    Hope it helps others.