Method to Get All Edittexts in a View

Method to get all EditTexts in a View

EDIT

MByD pointed me to an error, thus making my answer almost identical to that of blackbelt. I have edited mine to the correct approach.


You could do a for-each loop and then check if each view is of the type EditText:

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

for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
myEditTextList.add( (EditText) myLayout.getChildAt( i ) );

You could also, instead of having a list of EditTexts, have a list of ID's and then just add the id of the child to the list: myIdList.add( child.getId() );


To access your layout you need to get a reference for it. This means you need to provide an ID for your layout in your XML:

<LinearLayout android:id="@+id/myLinearLayout" >
//Here is where your EditTexts would be declared
</LinearLayout>

Then when you inflate the layout in your activity you just make sure to save a reference to it:

LinearLayout myLinearLayout;

public void onCreate( Bundle savedInstanceState ) {
super( savedInstanceState );
setContentView( R.layout.myLayoutWithEditTexts );

...

myLinearLayout = (LinearLayout) findViewById( R.id.myLinearLayout );
}

You then have a reference to your the holder of your EditTexts within the activity.

Android Development: How To Get All EditText Children of a ViewGroup?

You would need to call findViewById on each of the LinearLayouts. If you do this, you can set the same ID for each EditText.

How get text of all editText?

Also similiar to: Method to get all EditTexts in a View

I would first state that if you only need to cycle over the edit texts, you can add each of their view id's to an array list like :

ArrayList<EditText> list = new ArrayList<EditTexts>();
EditText edit = (EditText) view.findViewById(R.id.X);
list.add(edit);

Then loop over them to get the text you need, like:

for (EditText edit : list){
String text = edit.getText().toString();
}

-Or if you need to use the table layout you defined then have you tried removing the ending findViewById on your last line?

TableLayout or = (TableLayout) findViewById(R.id.TableLayoutBells); 
EditText editTextBells = (EditText) or.getChildAt(i);

Can I get all EditText of an android layout at once?

Try using ViewGroup

 ViewGroup group = (ViewGroup)findViewById(R.id.your_group);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");//here it will be clear all the EditText field
}
}

Android - Loop to collect all editText values

You can try this by creating an array of the ids of the textview and looping through the array.
Try this:

int[] ids = new int[]{R.id.text1Value,R.id.text2Value,R.id.text3Value};//and so on
int i =1;for(int id : ids){ EditText t = (EditText) findViewById(id); String Ref = "text" + i; editor.putString(Ref, t.getText().toString()); i++;}

loop thru all edittext views in activity

You can add all editTexts to an array or a list:

EditText[] ets = new EditText[nbr];
ets[0] = edittext1;
ets[1] = edittext1;
....

and then loop over them with:

for (EditText et : ets) {
if (!et.getText().toString().isEmpty()) {
//do something
}
}


Related Topics



Leave a reply



Submit