I have looked at other questions regarding this problem but the solutions don’t work for me. I have a number of edittext fields on a page and want to be able to reset them with one button press. I implemented the solution from this thread but I have nested linear layouts and it only clears the edittext that is a direct child of the main container. How can I get it to find all of the edittext fields or will I have to reference each one?
My xml file structure is as follows:
<ScrollView>
<LinearLayout android:id="@+id/MainParent">
<LinearLayout>
TextView
EditText
</LinearLayout>
<LinearLayout>
TextView
EditText
</LinearLayout>
<LinearLayout>
TextView
EditText
</LinearLayout>
EditText
<LinearLayout>
Button1
Button2
</LinearLayout>
</LinearLayout>
</ScrollView>
Snippet from Java File:
ViewGroup group = (ViewGroup)findViewById(R.id.MainParent);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
If you are not interested in referencing each EditText specifically, I’d make a recursive function to handle the hierarchy.
Then you just call: