Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method to get all EditTexts in a View

Tags:

android

can anyone help me with coding a method to get all EditTexts in a view? I would like to implement the solution htafoya posted here: How to hide soft keyboard on android after clicking outside EditText?

Unfortunately the getFields() method is missing and htafoya did not answer our request to share his getFields() method.

like image 370
Reto Avatar asked Oct 17 '11 07:10

Reto


3 Answers

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.

like image 106
kaspermoerch Avatar answered Oct 19 '22 09:10

kaspermoerch


Here's a method I wrote to recursively check all EditText children of a ViewGroup, handy for a long sign-up form I had to do and probably more maintainable.

private EditText traverseEditTexts(ViewGroup v)
{
    EditText invalid = null;
    for (int i = 0; i < v.getChildCount(); i++)
    {
        Object child = v.getChildAt(i); 
        if (child instanceof EditText)
        {
            EditText e = (EditText)child;
            if(e.getText().length() == 0)    // Whatever logic here to determine if valid.
            {
                return e;   // Stops at first invalid one. But you could add this to a list.
            }
        }
        else if(child instanceof ViewGroup)
        {
            invalid = traverseEditTexts((ViewGroup)child);  // Recursive call.
            if(invalid != null)
            {
                break;
            }
        }
    }
    return invalid;
}

private boolean validateFields()
{   
    EditText emptyText = traverseEditTexts(mainLayout);
    if(emptyText != null)
    {
        Toast.makeText(this, "This field cannot be empty.", Toast.LENGTH_SHORT).show();
        emptyText.requestFocus();      // Scrolls view to this field.
    }
    return emptyText == null;
}
like image 28
Jarrod Smith Avatar answered Oct 19 '22 11:10

Jarrod Smith


You can do it by calling View#getFocusables, which will return an arraylist of all focusable views in a View.

Then you can either check if they are EditTexts, with (instanceof) or act on all of them.

like image 34
MByD Avatar answered Oct 19 '22 10:10

MByD