3

idI am developeing an app in android, In the layout i am using more than 20 EditText. To, reduce the size of my coding i was trying to implement EditText in an array .So, help me with how to implement the Edittext in an array and also how i should name the EditText in XML file to use it as an array.

I tried to implement this code but this is not working,

edittext reference in java file EditText[] et1 = new EditText[20];

and putting this into a for loop,

for(i=0;i<20;i++)
{
et1[i]=(EditText)findViewById(R.id.edittext[i]);
}

but eclipse cannot properly resolve edittext[i].

tell me How do I properly create an array of EditText in my xml so that it is recognized

5 Answers 5

1

You can do something like this:

ArrayList<EditText> editTextList = new ArrayList<EditText>();

for(int i = 0; i < rootLayout.getChildCount(); i++) {
    if(rootLayout.getChildAt(i) instanceof EditText) {
        editTextList.add( (EditText) rootLayout.getChildAt(i));
    }
}

Where rootLayout is the View containing all EditTexts (a LinearLayout, for example)

Sign up to request clarification or add additional context in comments.

Comments

0

you have to create edittext programaticaly as shown below:-

private EditText[] mEditTextPlayers;

public void goButtonClicked(View view) {
    maalContainer.removeAllViews();
    int numPlayers = Integer.parseInt((String) numberOfPlayers.getSelectedItem());

    LayoutInflater inflater = LayoutInflater.from(view.getContext());
    mEditTextPlayers = new EditText[numPlayers];

    for (int i = 0; i < numPlayers; i++) {
        //Pass the parent as the second parameter to retain layout attributes
        mEditTextPlayers[i] = inflater.inflate(R.layout.player_entry_item, maalContainer, false);
        maalContainer.addView(dynamicEntryView);
    }
}

Please let me know if it works for you or not :):-

Comments

0

I'll give a couple examples. First and foremost, this line is incorrect:

et1[i]=(EditText)findViewById(R.Layout.edittext[i]);

This is not how the findViewById() method takes a parameter. You can't append R.id to an array.

Now, you could do something like this: id[] = new id[] {R.Layout.editTextOne, R.Layout.editTextTwo}

When we sit back and think about this, we may realize it's going to take as much code (or more) as doing the following:

    EditText[] editTextArray = new EditText[] {
            (EditText) findViewById(R.id.oneEditText),
            (EditText) findViewById(R.id.twoEditText),
            (EditText) findViewById(R.id.threeEditText),
            (EditText) findViewById(R.id.fourEditText),
            (EditText) findViewById(R.id.fiveEditText),
            (EditText) findViewById(R.id.sixEditText),
            (EditText) findViewById(R.id.sevenEditText),
            (EditText) findViewById(R.id.eightEditText),
            (EditText) findViewById(R.id.nineEditText),
            (EditText) findViewById(R.id.zeroEditText) };

    for (int x = 0; x < editTextArray.length; x++) {
        editTextArray[x].setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            // DO stuff here........................
            }
        });
    }

Lastly, DO NOT alter the xml file. Leave it be.

Comments

0

First of all encapsulate your all of your 20 EditText inside a layout and provide that layout with an id something like:

<LinearLayout
    android:id="@+id/holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText android:id="@+id/edittext1"/>
    <EditText android:id="@+id/edittext2"/>
    <EditText android:id="@+id/edittext3"/>
    ...
    <EditText android:id="@+id/edittext20"/>
</LinearLayout>

Now inside your activity do something like this:

//Global variable
int index = 0;
EditText[] editTextArray = new EditText[20];

//inside activity
LinearLayout holder = (LinearLayout) findViewById(R.id.holder)
...
for(int i=0; i<holder.getChildCount(); i++){
    View view = holder.getChildAt(i);
    if(view  instanceof EditText){
        editTextArray[index++] = view;
    }
}

I have used LinearLayout for this example but you can use any layout.

Comments

0

Here is a solution that works. Suppose you have a layout (id = "R1") with 9 EditText widgets. The following code will set up an array allowing you to access those widgets in an array:

    EditText[] etArray = new EditText[9];
    int index = 0;
    //store the EditText fields in an array
    LinearLayout LL = findViewById(R.id.R1);
    for(int i = 0; i < LL.getChildCount(); i++) {
        View v = LL.getChildAt(i);
        if (v instanceof EditText) {
            //the following is the crucial line missing from the other answers
            etArray[index++] = ((EditText) v)
        }
    }

Now, you can access the elements of the array as in the following example:

    for(int i = 0; i < 9; i++) {
        etArray[i].setText(Integer.toString(i));
    }

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.