I'm adding EditText in linear layout and it gives a view like that in image.
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.
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()));
}
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();
}
}
Get the editext
from your list editTextList
String data = editTextList.get(index).getText().toString();
Add check for editTextList should not be null or empty.
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()));
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.