Junaid
Junaid

Reputation: 336

How to get data from many EditTexts when Its added in layout programmatically in android

I'm adding EditText in linear layout and it gives a view like that in image.enter image description here

I'm getting this view by using this code.

public class SearchRecipe extends AppCompatActivity {
    LinearLayout parentLayout;
    ImageButton searchRecipe;
    private int EDITTEXT_ID = 1;
    private List<EditText> editTextList;
    EditText editTextItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_recipe);
        setActionBar();
        init();
        searchRecipe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editTextItem = (EditText) parentLayout.findViewById(EDITTEXT_ID);
                for (int i = 0; i < editTextList.size(); i++) {
                    Log.e("All Values=", editTextList.get(i).getText().toString());
                    Toast.makeText(SearchRecipe.this, editTextItem.getText().toString() + " ", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void init() {
        parentLayout = (LinearLayout) findViewById(R.id.parent_layout); //make sure you have set vertical orientation attribute on your xml
        searchRecipe = (ImageButton) findViewById(R.id.search_button);
        editTextList = new ArrayList<EditText>();

        TextView addMoreText = new TextView(this);
        addMoreText.setText("Add More Ingredients");
        addMoreText.setGravity(Gravity.CENTER);
        addMoreText.setPadding(20, 20, 20, 20);
        addMoreText.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.add, 0);
        addMoreText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editTextItem = new EditText(SearchRecipe.this);
                editTextItem.setId(EDITTEXT_ID);
                editTextList.add(editTextItem);
                EDITTEXT_ID++;
                editTextItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.cross, 0);
                editTextItem.setPadding(20, 20, 20, 20);
                editTextItem.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        parentLayout.removeView(editTextItem);
                        return true;
                    }
                });

                parentLayout.addView(editTextItem, 0);
            }
        });

        parentLayout.addView(addMoreText);
    }

Now the only problem I'm facing is that. I'm not getting the text from edittext properly. Let me Explain what I want to do.

  1. Click on Add More TextView will add one more edit text.
  2. After adding all edittexts I will click on Search button.
  3. By clicking search button will get the data from edittexs and save in arraylist. I tried a lot but can't do this properly. will you please help me to do this thing ? I'm stuck in from many days.

Upvotes: 0

Views: 86

Answers (4)

Vishal Thakkar
Vishal Thakkar

Reputation: 2127

you can do like below if view inside fragment.

  public static String getText(final Activity activity)   {
final StringBuilder stringBuilder=new StringBuilder();

           LinearLayout scrollViewlinerLayout = (LinearLayout) activity.findViewById(R.id.linearLayoutForm);
          ArrayList<String> msg = new ArrayList<String>();
           for (int i = 0; i < scrollViewlinerLayout.getChildCount(); i++)
            {
               LinearLayout innerLayout = (LinearLayout) scrollViewlinerLayout.getChildAt(i);
              EditText editText = (EditText) innerLayout.findViewById(R.id.meeting_dialog_et);
                msg.add(editText.getText().toString());
            }
           for (int j=0;j<msg.size();j++)
           {
               stringBuilder.append(msg.get(j)).append(";");
            }
            Toast t = Toast.makeText(activity.getApplicationContext(), stringBuilder.toString(), Toast.LENGTH_SHORT);
            t.show();

    return stringBuilder.toString();
}

there is another way is make arraylist Edittexes and add each edittext when it added to layout. then you can get like below:

for (int i = 0; i < Edittexes.size(); i++) {
                    if (Edittexes.get(i) == view)
                    {   
                         String text=Edittexes.get(i).getText(); 


                }

                }

Upvotes: 0

Shrenik Shah
Shrenik Shah

Reputation: 1990

if you are createing edit text run time only for this purpose then there is no need of below tow lines

editTextItem.setId(EDITTEXT_ID);
EDITTEXT_ID++;

To retrive data from each edit box follow below things

for (EditText editText : editTextList) {
       /* now you can get the value from Edit-text and save in the ArrayList
          or you can append it in same string*/
        yourArraList.add(editText.getText().toString()));
    }

Upvotes: 1

Pradeep Gupta
Pradeep Gupta

Reputation: 1770

You can iterate over list using for-each loop

for (EditText editText : editTextList) {
           // now you can get the value from Edit-text and save in the ArrayList
            yourArraList.add(editText.getText().toString()));
        }

Upvotes: 0

Sohail Zahid
Sohail Zahid

Reputation: 8149

Get the editext from your list editTextList

String data = editTextList.get(index).getText().toString();

Add check for editTextList should not be null or empty.

Upvotes: 0

Related Questions