1

I made these codes. every time I click on the button, an EditText inflate in scroll layout. How can I get the ID for each EditText I inflate it ? to take the value of each of them ?

ScrollView scrollview;
LinearLayout  linearLayout;
LinearLayout.LayoutParams layoutParams;
//EditText hello;
static int i = 0;

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



    Button sum_1 = (Button)findViewById(R.id.sum);


    scrollview = (ScrollView)findViewById(R.id.scrollview);
    linearLayout = (LinearLayout)findViewById(R.id.gamehistory);
    Button b = (Button)findViewById(R.id.Button01);
    layoutParams = new LinearLayout.LayoutParams
            (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

    b.setOnClickListener(new View.OnClickListener(){


        public void onClick(View v) {



                EditText hello = new EditText(MainActivity.this);

                hello.setText("enter value" + i++);
                linearLayout.addView(hello, layoutParams);

                hello.setId(i++);


        }

    });
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
Prince
  • 49
  • 1
  • 4
  • When ever you are inflating EditText you can assign an ID using setId(int currentId). This curerntId will be static value which increased by 1 whenever you are inflating it. – Roll no1 Jan 20 '14 at 08:47

2 Answers2

1

the only reason you want to add an ID to the layouts is so that you can get a hold of that view in the code.. here you allready have access to the EditText

its easier then to just add all the EditTexts to List then use them from there or even to a EditText[]

LeMoN.xaH
  • 571
  • 2
  • 9
0
        b.setOnClickListener(new View.OnClickListener(){


        public void onClick(View v) {



                EditText hello = new EditText(MainActivity.this);

                hello.setText("enter value" + i++);
                linearLayout.addView(hello, layoutParams);

                hello.setId(i++);

               hello.setTag("edit");
        }

    });



int childcount = linearLayout.getChildCount();
    for (int i = 0; i < childcount; i++) {
             View v1 = linearLayout.getChildAt(i);
            if(v1.getTag.equls("edit"){

                    System.out.println(v1..getId());
             }

    }
Digvesh Patel
  • 6,503
  • 1
  • 20
  • 34