How to iterate over view elements

I have a view with radios, inputs and a button, and when I click on it, I want to check that all the inputs contain information. How can I iterate over the presentation elements in action and check if each text view meets the above requirement? Thank.

+49
android forms
Jan 26 '11 at 20:45
source share
3 answers

I did something similar in some code that I don’t have right now, but from memory it should be something like this (assuming the parent view of LinearLayout with the identifier "layout"):

LinearLayout layout = (LinearLayout)findViewById(R.id.layout); boolean success = formIsValid(layout); public boolean formIsValid(LinearLayout layout) { for (int i = 0; i < layout.getChildCount(); i++) { View v = layout.getChildAt(i); if (v instanceof EditText) { //validate your EditText here } else if (v instanceof RadioButton) { //validate RadioButton } //etc. If it fails anywhere, just return false. } return true; } 
+105
Jan 26 2018-11-21T00:
source share

To apply the kcoppock method recursively, you can change it to this:

 private void loopViews(ViewGroup view) { for (int i = 0; i < view.getChildCount(); i++) { View v = view.getChildAt(i); if (v instanceof EditText) { // Do something } else if (v instanceof ViewGroup) { this.loopViews((ViewGroup) v); } } } 
+13
Jun 18 '16 at 9:14
source share

Your onClickListener provides a View v object; use View rV = v.getRootView() to put yourself on the form. Then use rV.findViewWithTag( ... ) or rV.findViewByID(R.id. ... ) to find form elements.

0
Jan 26 2018-11-21T00:
source share



All Articles