I'm parsing layout file with XmlPullParser
and I need to count inner tags to be able recognize children views. I found the method to count attributes for each tag, but cannot find how to count exactly tags.
I have code like this one:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0d3fff"
android:text="New Button 1"
android:layout_marginRight="10dp"
android:id="@+id/btnDef1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDef2"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tv1"/>
</LinearLayout>
Anyone has ideas how to count children for tag LinearLayout
?
Thanks in advance.
I have no Idea why anyone would want to count tags from a Layout file. If you are inflating the layout use either the getChildCount method or use the following code to get count of child of LinearLayout
int childcount = ll.getChildCount();
for (int i=0; i < childcount; i++){
View v = ll.getChildAt(i);
}
If you are inclined on using XMLPullparser, I dont think there is any direct method to count xml Tags. You have to go through each tag
int count = 0;
XmlPullParser xpp = factory.newPullParser();
try {
xpp.setInput(new InputStreamReader(is));
int eventType = xpp.next();
Project p = null;
while(eventType != XmlPullParser.END_DOCUMENT) {
count++;
String tagName = xpp.getName();
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
break;
// and other cases that you want to handle
}
}
} catch (Exception e) {
e.printStackTrace();
}