-1

Is it possible to add a new EditText automatically in a ListView on a Button click?

If so please let me know.

AndyN
  • 1,742
  • 1
  • 15
  • 30
shubham0703
  • 59
  • 2
  • 12
  • Possible duplicate of [How to Programmatically Add Views to Views](https://stackoverflow.com/questions/2395769/how-to-programmatically-add-views-to-views) – Ricardo A. May 03 '19 at 11:53

3 Answers3

0
EditText name =  new EditText(youractivity);
convertView.addView(name);

Call notifydatasetchanged() but you have to save the field that you add becouse after scrolling into the list you will have in all list that editText and you have to remove where you don't want to appear.

What I recommand you is to make an editext into your cellView hidden and make it visible when you tap on your button.

Lucian Novac
  • 1,255
  • 12
  • 18
  • i have by default one edit text but what i want is that at run time instead of having default amount of edit text i want any number of edit text to get created on a button click. So instead of hardcoding default values i want it to be dynamic. – shubham0703 Mar 26 '14 at 09:17
  • Then implement the first part but you have to play with the "replicate problem" into other cells – Lucian Novac Mar 26 '14 at 09:20
0

I would like to suggest one code its not about listview but see if it could give you some idea,by adding views dynamically in linear layout which is inside scroll view.

public class MainActivity extends Activity {

    ScrollView scrollview;
    LinearLayout  linearLayout;
    LinearLayout.LayoutParams layoutParams;
    static int i;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        scrollview = (ScrollView)findViewById(R.id.scrollview);
        linearLayout = (LinearLayout)findViewById(R.id.linearlayout);
        Button button = (Button)findViewById(R.id.button);
        layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        button.setOnClickListener(new OnClickListener(){

            public void onClick(View v) {
                TextView view = new TextView(MainActivity.this);             
                view.setText(++i+" view");
                linearLayout.addView(view, layoutParams); 
            }

        });

}}
Harshal Benake
  • 2,391
  • 1
  • 23
  • 40
0

You could make a custom adapter for your listview and give it a layout containing the edittext with the visibility set to gone. You can then set it to visible onbuttonclick.

Hbibna
  • 568
  • 7
  • 11