Android - get children inside a View?
for(int index = 0; index < ((ViewGroup) viewGroup).getChildCount(); index++) {
View nextChild = ((ViewGroup) viewGroup).getChildAt(index);
}
Will that do?
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.
// …
}
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.
Find all child views for given root view recursively
private List<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);
//Do not add any parents, just add child elements
result.addAll(getAllChildren(child));
}
return result;
}
Find child of a LinearLayout inside a LinearLayout
I think I found the answer to it. You need to change a little bit of code in the startGame() method I m providing the code for startGame below.
public void startGame(View view) {
LinearLayout ll = findViewById(R.id.playerView);
List text = new ArrayList();
for (int loop = 0; loop < ll.getChildCount(); loop++) {
//this is the code in question and where I want to get the text from
//all my EditTexts
LinearLayout inner = (LinearLayout) ll.getChildAt(loop);
for (int j = 0; j < inner.getChildCount(); j++) {
if (inner.getChildAt(j) instanceof EditText) {
EditText textET = (EditText) inner.getChildAt(j);
Log.d("TAG",textET.getText().toString());
}
}
}
}
In the above code you were able to get the first child only but as you have added a linearLayout with orientation Horizontal in a parent LinearLayout with orientation Vertical, you have written code for the child of parent layout i.e playerView. I have modified the code to get the elements of the child Linear layout and Log prints all the text from the EditText.
Hope that helps!!
ViewGroup How to get child view by location (x, y)?
Use this Utils class bellow. Only 1 method call needed
No need to subclass your layout. Call that method from main-thread, it supports translation/rotation/scale as well.
// return null if no child at the position is found
View outputView = Utils.findChildByPosition(theParentViewGroup, x, y)
Full Source code of class Utils:
public final class Utils {
/**
* find child View in a ViewGroup by its position (x, y)
*
* @param parent the viewgourp
* @param x the x position in parent
* @param y the y position in parent
* @return null if not found
*/
public static View findChildByPosition(ViewGroup parent, float x, float y) {
int count = parent.getChildCount();
for (int i = count - 1; i >= 0; i--) {
View child = parent.getChildAt(i);
if (child.getVisibility() == View.VISIBLE) {
if (isPositionInChildView(parent, child, x, y)) {
return child;
}
}
}
return null;
}
private static boolean isPositionInChildView(ViewGroup parent, View child, float x, float y) {
sPoint[0] = x + parent.getScrollX() - child.getLeft();
sPoint[1] = y + parent.getScrollY() - child.getTop();
Matrix childMatrix = child.getMatrix();
if (!childMatrix.isIdentity()) {
childMatrix.invert(sInvMatrix);
sInvMatrix.mapPoints(sPoint);
}
x = sPoint[0];
y = sPoint[1];
return x >= 0 && y >= 0 && x < child.getWidth() && y < child.getHeight();
}
private static Matrix sInvMatrix = new Matrix();
private static float[] sPoint = new float[2];
}
Related Topics
Bitmap Recycle with Largeheap Enabled
Bind Service to Activity in Android
Why Is Android Studio Reporting "Uri Is Not Registered"
The Signing Fingerprint You Specified Is Already Used by Another Android Oauth2 Client
How Do Detect Android Tablets in General. Useragent
Is There a Listener for When the Webview Displays It's Content
How to Free a Component in Android/Ios
How to Get City Name from Latitude and Longitude Coordinates in Google Maps
How to Implement a View Holder
Android Hide/Unhide App Icon Programmatically
Using Global Exception Handling on Android
Detect an Application Is Installed or Not
Noclassdeffounderror When Googleanalyticstracker.Getinstance()
Add New Item Count to Icon on Button - Android
Xamarin.Forms Listview: Set the Highlight Color of a Tapped Item