SANJEEV REDDY
SANJEEV REDDY

Reputation: 37

How to take values from dynamically created EditText?

Here I have created EdiIexts dynamically by clicking the button, how can I take values from these EditTexts? I have seen many examples but I am unable to get values!

 final LinearLayout ll=new LinearLayout(this);
 ll.setOrientation(LinearLayout.VERTICAL);
 Button add_btn=new Button(this);
 add_btn.setText("Click to add TextViiews and EditTexts");
 ll.addView(add_btn);     

 add_btn.setOnClickListener(new OnClickListener() {     
     public void onClick(View v) {

         EditText et=new EditText(getApplicationContext());

         ll.addView(et);

Upvotes: 0

Views: 3703

Answers (4)

Priya Jagtap
Priya Jagtap

Reputation: 1023

try this,

LinearLayout linearLayoutForm = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
EditText edit = (EditText) linearLayoutForm.findViewById(id);
String value = edit.getText().toString();

Upvotes: 0

Nirav Tukadiya
Nirav Tukadiya

Reputation: 3417

add this code where you want to get the edittext's value

 EditText et2=(EditText)ll.getChildAt(l1.getChildCount()); //make sure to create new edittext variable do not use "et"
 String s=et2.getText().toString();

Upvotes: 2

user370305
user370305

Reputation: 109237

Declare EditText et as a Class level Member variables for your Activity,

private EditText et = null;

Now, in Button's onClick

add_btn.setOnClickListener(new OnClickListener() {     
 public void onClick(View v) {
     et = new EditText(getApplicationContext());
     ll.addView(et);

Now, you can get the EditText et values any where in your Activity scope, using

if(et != null)
String value = et.getText().toString();

Upvotes: 1

MysticMagicϡ
MysticMagicϡ

Reputation: 28823

You can get dynamically created edittext's value the same way you would do with edittext of .xml file.

String value;
add_btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                value = et.getText().toString();
            }
        });

Upvotes: 2

Related Questions