How to get EditText ID - android

I have added some EditTexts using a loop,
textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);
for(int i=1; i <= 8; i++){
final EditText ed = new EditText(this);
ed.setText("" + i);
ed.setInputType(2);
ed.setLayoutParams(lparams);
textFieldsLayout.addView(ed);
}
}
After this I want to get the text that the user adds into the EditText fields, but I am stuck on how to do this. How would I get an Id for each of these EditText Fields?
Thanks, Oli

just add them to a collection when you're creating them that you can easily refer to by index or key or loop through or otherwise.
EditText[] etCollection = new EditText[8];
..........
textFieldsLayout = (LinearLayout) findViewById(R.id.LinearLayout2);
for(int i=1; i <= 8; i++){
final EditText ed = new EditText(this);
ed.setText("" + i);
ed.setInputType(2);
ed.setLayoutParams(lparams);
textFieldsLayout.addView(ed);
etCollection[i] = ed; <------ adding them to the collection
}
}

You'll have to do ed.setId( id ); on each one your create, and id should be unique for each one you want to find. When you go to find one do
EditText ed = (EditText)findViewById( id );
You'll have to keep track of your ids somewhere.
Alternatively you can just keep a list of the EditTexts that you create and run through the list to get the text of each one.

Related

How to create array of a specific view and add each of them to main layout in Android

I am able to run following code without any issue
EditText[] course_name = new EditText[4];
OnCreate {
ConstraintLayout layout = findViewById(R.id.layout_setupclass);
course_name[0] = new EditText(this);
course_name[0] = (EditText) LayoutInflater.from(this).inflate(R.layout.edittext, null);
layout.addView(course_name[0]);
}
But app would crash when I try this
EditText[] course_name = new EditText[4];
OnCreate {
ConstraintLayout layout = findViewById(R.id.layout_setupclass);
for(int i=0; i<5; i++)
{
course_name[i] = new EditText(this);
course_name[i] = (EditText) LayoutInflater.from(this).inflate(R.layout.edittext, null);
layout.addView(course_name[i]);
}
}
you are creating array with 4 elements capacity new EditText[4];, but in your iteration you have 5 in-for-loop calls (i=0 to 4). index 4 doesn't exists in array (arrays are 0-based indexed), so you are getting ArrayIndexOutOfBoundsException on course_name[i] call (when i=4, as max may be 3 due to size of array)
btw. calling course_name[i] = new EditText(this); is redundant, as in very next line you are attaching another (inflated from XML) view to this variable

What's the best practice for keeping track of dynamically generated EditText?

I have a quantity of items which I'm retrieving from a database. The user is expected to enter a serial number for every item, and the input values are to be posted back to the database. Thus, a dynamic EditText is to be generated for every item. I normally do it this way:
int itemQuantity;
List<EditText> listEditText = new ArrayList<EditText>();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
for(int i = 0; i < itemQuantity); i++){
EditText editText = new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.addView(editText);
listEditText.append(editText);
}
Then I use listEditText.get(index).getText().toString(); to retrieve values from each EditText.
Is this the right way of doing when considering performance and good practices?

android Edit Text Customization

for (int i = 0; i < sub.length; i++) {
tr[i] = new TableRow(this);
EditText et = new EditText(this);
et.setText(message2);
tr[i].addView(et);
EditText et1 = new EditText(this);
et1.setText(sub[i]);
tr[i].addView(et1);
EditText et2 = new EditText(this);
et2.setText(cde[i]);
tr[i].addView(et2);
EditText et3 = new EditText(this);
et3.setText(crd[i]);
tr[i].addView(et3);
ll.addView(tr[i]);
}
This is my code to create four edit text components in a row.
I need to give same text parameters to all the edittext components. But using methods to all four edittext components individually makes the code too lengthy.
Is there any solution so that I can use only one method to set text parameters for all the edittext components?
Using a method while adding your EditTexts to your TableRow can make your code easier to read.
private void addEditTextToTableRow(TableRow tableRow, String text) {
EditText editText = new EditText(this);
editText.setText(text);
tableRow.addView(editText);
}
Your for loop then becomes:
for (int i = 0; i < sub.length; i++) {
TableRow tr[i] = new TableRow(this);
addEditTextToTableRow(tr[i], message2);
addEditTextToTableRow(tr[i], sub[i]);
addEditTextToTableRow(tr[i], cde[i]);
addEditTextToTableRow(tr[i], crd[i]);
ll.addView(tr[i]);
}
try creating your components in the XML file, which will serve as a vision, and activity or in the XML Set The only text you need to set ... if you need utilzar one table, use the listView in XML and put all your EditText inside the listView

Generate number of Edit Text by storing andretrieve each value

I want to generate number of EditText column wise to store and retrieve each value .
Anyone can help me ?
I hope this may help you to resolve your problem.
EditText[] etxt = new EditText[Size];
EditText et;
//Generate and set values
for(int i=0;i<Size;i++)
{
et = new EditText(this);
et.setText(""+i)
et.setId(i);
etxt[i] = et;
//Add item to your view
//YourView.addView(etxt[i]);
}
Now you need to add to your View
//Get values
for(int s=0;s<etxt.lenght;s++)
{
etxt[s] = et;
Log.i("Test","Value: "+etxt[i].getText().toString());
}

Getting content from editText

In my application, i have a tableLayout with many editTexts in it. When i click "save"button, i want to access all the values entered in editTexts. I have assigned IDs in runtime while creating the table. Now how can i access the values from editTexts when "save" button is clicked...? I have used something like below to assign IDs,
for(int i=0;i< no_of_rows ;i++)
for(int j=0;j<5;j++)
{
...............
assignment.setId(i+j);
.............
}
Could anyone suggest a solution..?
Other nice solution is to cycle through all childrens of some Layout, so you don't need to set any special IDs. Just try:
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
child_count = layout.getChildCount();
for (int i=0; i<child_count; i++)
{
EditText text = (EditText) layout.getChildAt(i);
// do work with text
}
With some other code, you can do this for any other layout hierarchy.
How about something like:
ArrayList<String> strings = new ArrayList<String>();
for(int i=0;i< no_of_rows ;i++)
for(int j=0;j<5;j++)
{
EditText text = (EditText)ActivityName.this.findViewById(i+j);
strings.add(text.getText().toString());
}
}
This would give you all of the values from all of the texts in one big array. If that's not what you want, let me know and I'll see if I can adjust the code.

Categories

Resources