-
Notifications
You must be signed in to change notification settings - Fork 0
/
XMLLayoutParser.java
60 lines (48 loc) · 1.64 KB
/
XMLLayoutParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// package com.you.yourpackage
import java.io.IOException;
import java.util.LinkedList;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.Context;
import android.content.res.XmlResourceParser;
import android.util.Log;
import android.util.Xml;
public class XMLLayoutParser {
Context activity;
/*
* Each constant represents an Android View type
* Add in additional constants as needed; those here
* are just some that seem would be most commonly needed
*/
public static final String EDIT_TEXT = "EditText";
public static final String TEXT_VIEW = "TextView";
public static final String CHECKBOX = "CheckBox";
public XMLLayoutParser(Context act){
activity = act;
}
public int[] getElementIds(int layoutId, String viewType) throws XmlPullParserException, IOException{
XmlResourceParser parser = activity.getResources().getLayout(layoutId);
LinkedList<Integer> idList = new LinkedList<Integer>();
while(parser.getEventType()!=XmlResourceParser.END_DOCUMENT){
parser.next();
if(parser.getEventType()==XmlResourceParser.START_TAG){
if(parser.getName().equals(viewType)){
/* long viewId = parser.getIdAttributeResourceValue(0); // this is what's supposed to work
if(viewId!=0)
idList.add((int) viewId);
*/
// this is the workaround!
if(parser.getAttributeName(0).equals("id")){
long viewId = parser.getAttributeResourceValue(0, 0);
idList.add((int)viewId);
}
}
}
}
parser.close();
int[] inputIds = new int[idList.size()];
for(int j = 0;j<inputIds.length;j++)
inputIds[j] = idList.pollFirst();
return inputIds;
}
}