1

So I have a layout that gets added to the main layout every time the user presses a button. I got that working fine, but that layout happens to consist of several EditTexts. What would be the best way to get the text from the EditText? I only have the id of the layout itself, not the EditTexts inside the layout.

I thought of just adding EditTexts dynamically one by one, but is there a more efficient way of doing it? I'd much rather just inflate an xml layout every the button is clicked.

2
  • How many EditTexts? Is there limits? Commented Dec 13, 2012 at 2:15
  • there are 2 EditTexts and 2 TextViews in the layout that gets inflated. The TextViews don't matter. The user can add as many layouts as they want Commented Dec 13, 2012 at 2:22

2 Answers 2

1

I assume you're adding new views by inflating them and then adding them to the main view similar to below.

LinearLayout newView = (LinearLayout)this.getLayoutInflater().inflate(R.layout.my_layout, null);

LinearLayout mainView = (LinearLayout)this.findViewById(R.id.mainLayout);
mainView.addView(newView);

You can use findViewById() on the newView to access each EditText as required.

EditText editText = (EditText) newView.findViewById(R.id.myButton);
String text = editText.getText().toString();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for some reason I didn't realize that I could just get the ids one by one and give them a different reference.
0

Since it's a fixed layout you inflate from a particular XML file, you can also use getChildAt(int index) to find a particular view of the ViewGroup.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.