How to get data from each dynamically created EditText in Android?
In every iteration you are rewriting the ed
variable, so when loop is finished ed
only points to the last EditText instance you created.
You should store all references to all EditTexts:
EditText ed;
List<EditText> allEds = new ArrayList<EditText>();
for (int i = 0; i < count; i++) {
ed = new EditText(Activity2.this);
allEds.add(ed);
ed.setBackgroundResource(R.color.blackOpacity);
ed.setId(id);
ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
linear.addView(ed);
}
Now allEds
list hold references to all EditTexts, so you can iterate it and get all the data.
Update:
As per request:
String[] strings = new String[](allEds.size());
for(int i=0; i < allEds.size(); i++){
string[i] = allEds.get(i).getText().toString();
}
How to get data from dynamically created EditText using LayoutInflater in android?
public void goButtonClicked(View view) {
int numberofPlayersTolayout = Integer.parseInt(numberOfPlayers.getSelectedItem().toString());
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
maalContainer.removeAllViews();
maalText = new EditText[numberofPlayersTolayout];
points = new EditText[numberofPlayersTolayout];
result = new TextView[numberofPlayersTolayout];
for (int i = 0; i < numberofPlayersTolayout; i++) {
View dynamicEntryView = inflater.inflate(R.layout.player_entry_item, null);
maalContainer.addView(dynamicEntryView, params);
TextView playerName = (TextView) dynamicEntryView.findViewById(R.id.player_name_textview);
playerName.setText("Player :" + (i + 1));
maalText[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_maal);
points[i] = (EditText) dynamicEntryView.findViewById(R.id.player_item_edittext_point);
result[i] = (TextView) dynamicEntryView.findViewById(R.id.player_item_textview_result);
}
How to get text from all EditText Dynamically added in a row
You could retrieve the value by looping the parentView, something like this
ArrayList getEditTextList()
{
ArrayList<String> list = new ArrayList<String>();
int size = parentLinearLayout.getChildCount()
for (int i=0 ; i < size ; i++)
{
View view = parentLinearLayout.getChildAt(i);
EditText text = view.findViewById(R.id.yourEditTextName);
list.add(text.getText().toString());
}
return list;
}
Later you can add the values to Shared preference either by converting Arraylist to Json String or Adding it one by one.
How to get value from dynamically created edit text in Android?
Although, I didn't get what exactly your question is, still I'll try to help.
What exactly this code does is, on clicking the button on top an new layout(one that you wanted) will add to the activity screen at runtime, i.e an edittext with a button, and when you click on that button, A toast with the value of respective edittext will be shown. Hence solving the problem of getting the value of dynamically added edittext
MainActivity
public class MainActivity extends AppCompatActivity {
Button generateET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout myLinearLay = (LinearLayout) findViewById(R.id.dynamic);
generateET = (Button) findViewById(R.id.generateBtn);
generateET.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater l = getLayoutInflater();
final View viewToAdd = l.inflate(R.layout.to_add, null);
Button buttonWayPoints = (Button) viewToAdd.findViewById(R.id.btn_waypoint);
final EditText editTextWayPoints = (EditText) viewToAdd.findViewById(R.id.et_waypoint);
buttonWayPoints.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editTextWayPoints.getText().toString() != null) {
Toast.makeText(MainActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No text found", Toast.LENGTH_SHORT).show();
}
}
});
myLinearLay.addView(viewToAdd);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.techmahindra.stackques.MainActivity">
<Button
android:id="@+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="add editText To layout" />
<ScrollView
android:layout_below="@+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="@+id/dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
></LinearLayout>
</ScrollView>
</RelativeLayout>
to_add.xml(layout which will be inflated at runtime)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="@+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
Fetch data from dynamically created TextInputEditText and insert them to an array
I was able to resolve it in following way, thanks for the questioning hellboy and blackapps, it made me think bit differently.
private void fetchCertificates(){
ArrayList<String> certs = new ArrayList<>();
for(int i =0;i<linearLayout.getChildCount();i++){
View certificateView = linearLayout.getChildAt(i);
TextInputEditText newCerts = certificateView.findViewById(R.id.new_certs);
String name = newCerts.getText().toString();
certs.add(name);
}
String certList = android.text.TextUtils.join(",", certs);
Log.i("Certificates",certs);
}
Get data from each dynamically created EditText
While creating every new EditText
, you generate ids by setId()
but I don't see how these are going to be useful in your code.
Instead set the tag of the EditText
like this:
ed.setTag(allEds.size());
and add it to the list:
allEds.add(ed);
Now in each EditText
's tag you have stored its ordinal number (zero based) in the list and when you want to store its value in SharedPreferences
you can do:
SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
editor.putString("key" + et.getTag().toString(), ed.getText().toString());
editor.commit();
or by using a loop through all the items in the list:
for (EditText ed : allEds) {
editor.putString("key" + et.getTag().toString(), ed.getText().toString());
}
editor.commit();
This way you stored all the text values of the EditTexts with keys like:
"key0", "key1", "key2",...
Edit
Of you want to open another activity and pass the list to it:
Intent intent = new Intent(this, AnotherActivity.class);
ArrayList<String> allTexts = new ArrayList<>();
for (EditText e : allEds) {
allTexts.add(e.getText().toString())
}
intent.putExtra("Text", (Serializable) allTexts);
and in the other activity's onCreate()
:
ArrayList<String> list = (ArrayList<String>) getIntent().getSerializableExtra("Texts");
Edit2
Replace the code in btnSave.setOnClickListener()
and the lines below, with this code:
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
for (EditText ed : allEds) {
editor.putString("key" + ed.getTag().toString(), ed.getText().toString());
}
editor.commit();
Intent intent = new Intent(this, MainActivity.class);
ArrayList<String> allTexts = new ArrayList<>();
for (EditText e : allEds) {
allTexts.add(e.getText().toString());
}
intent.putExtra("Text", (Serializable) allTexts);
startActivity(intent);
}
});
Get text from dynamically created EditText on click of a button
You are passing to the getOnClickDoSomething
function the text of the newly created editText, so understandably it will be blank. What you may want is to change getOnClickDoSomething
, so that it accepts reference to the editTexts themselves, and later you can retrieve the their current text. (You will have to be careful not to leak views, as you have anonymous classes holding on to the Views.)
Example: Just change the following lines as follows
bt_plus.setOnClickListener(getOnClickDoSomething(et_one, et_two)); }
View.OnClickListener getOnClickDoSomething(final EditText one1,final EditText two1)
Log.e("========","=====one ="+one1.getText().toString().trim(),+"=========two = "+two1.getText().toString().trim());
Related Topics
Meaning of the Import Statement in a Java File
When/Why to Call System.Out.Flush() in Java
Why Can Not I Add Two Bytes and Get an Int and I Can Add Two Final Bytes Get a Byte
Can CSS in Javafx Be Used for Animations
Javafx - Border Radius <-> Background Color
Java, Find Intersection of Two Arrays
Scanner Doesn't See After Space
Use of Initializers VS Constructors in Java
How to Send Put, Delete Http Request in Httpurlconnection
Intellij Fat One Jar Artifact Does Not Generate a Working Output
Turning Off Hibernate Logging Console Output