Loop Through All Subviews of an Android View

Loop through all subviews of an Android view?

@jqpubliq Is right but if you really want to go through all Views you can simply use the getChildCount() and getChildAt() methods from ViewGroup. A simple recursive method will do the rest.

How to iterate through a view's elements

I've done something similar in some code I don't have with me at the moment, but from memory it should be something like this (assuming a parent view LinearLayout with an id of "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;
}

Iterating through viewgroup

I get the view count and then use that as a
counter to call getChildAt(int index)

Android | Get all children elements of a ViewGroup

(source)

If you want to get all the child views, as well as the views within children ViewGroups, you must do it recursively, since there is no provision in the API to do this out of the box.

private ArrayList<View> getAllChildren(View v) {

if (!(v instanceof ViewGroup)) {
ArrayList<View> viewArrayList = new ArrayList<View>();
viewArrayList.add(v);
return viewArrayList;
}

ArrayList<View> result = new ArrayList<View>();

ViewGroup viewGroup = (ViewGroup) v;
for (int i = 0; i < viewGroup.getChildCount(); i++) {

View child = viewGroup.getChildAt(i);

ArrayList<View> viewArrayList = new ArrayList<View>();
viewArrayList.add(v);
viewArrayList.addAll(getAllChildren(child));

result.addAll(viewArrayList);
}
return result;
}

This will give you an ArrayList with all the Views in the hierarchy which you can then iterate over.

Essentially, this code call itself if it finds another ViewGroup in the hierarchy, and then returns an ArrayList to be added to the bigger ArrayList.

How to send message to all subviews of a certain kind in view IOS?

I can't imagine why you wouldn't want to just group those all into an array, but you could loop through the views and use isKindOfClass: to check them. For example:

for (UIView *subview in self.view.subviews)
{
if ([subview isKindOfClass:[MyCustomClass class]])
{
// Do stuff
}
}

Get all child views inside LinearLayout at once

Use getChildCount() and getChildAt(int index).

Example:

LinearLayout ll = …
final int childCount = ll.getChildCount();
for (int i = 0; i < childCount; i++) {
View v = ll.getChildAt(i);
// Do something with v.
// …
}

Creating a view in which all intents are opened

What you could do is have just one Activity and then run everything else in Fragments.

All your Activity what need to handle is replacing the content Fragment.

I find myself short on words right now, so if you have any questions as to what I mean, feel free to shoot.

RemoteViews id in loops?

RemoteViews is not a View nor does it contain Views.

It is a way to "describe" a View and have it created in some other process. Basically it's just a set of actions that must be performed to build a View.

As there are no Views inside it there is no way to retrieve/iterate a list of subViews.



Related Topics



Leave a reply



Submit