augustine
augustine

Reputation: 1022

dynamic create editbox in android?

I want to create a Editbox dynamically when i click a add button and i also want to get the values typed in that Editbox when i click in save button.

Please Help me. Regards. Augustine

Upvotes: 1

Views: 3022

Answers (4)

Dave Morris
Dave Morris

Reputation: 569

To get the values typed into the EditText, you need to additionally set an identifier for the view.

     lEditText.setId(2); //you can use any integer ID

Then, you can retrieve the text inside the OnClickListener of the save button as:

EditText lEditText = (EditText)findViewById(2);
String txt = lEditText.getText().toString();

Upvotes: 2

Sujit
Sujit

Reputation: 10632

you can create EditText with the following code inside your Activity

EditText _edit = new EditText(this);

Then to add this to you activity layout you have to get the particular layout by it id For Ex.

LinearLayout linear = (LinearLayout)findViewById(R.id.linear);

then simple add this EditText object to the LinearLauout by using the following code..

linear.addView(_edit);

Upvotes: 1

Vinayak Bevinakatti
Vinayak Bevinakatti

Reputation: 40513

Try out this:

LinearLayout mLinearLayout = new LinearLayout(this);
mLinearLayout = (LinearLayout)findViewById(R.id.mylinearlayout);

Button lButton = (Button)findViewById(R.id.mybtnid);
lButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
            EditText lEditText = new EditText(this);
            lEditText.SetText("Text Here");
            mLinearLayout.addView(lEditText);
        }
    }

Upvotes: 3

mudit
mudit

Reputation: 25534

To create an edit text dynamically or programmatically:

EditText ed = new EditText(context);

Set whatever parameters you want to set for this edit text and then add this in your view:

view.addView(ed);

OR

view.addView(ed, layoutParams);

Upvotes: 1

Related Questions