Reputation: 3162
I have layout that I build using Java - not from XML file.
I'm assigning to each view an arbitrary id through the Java code:
For instance:
Button btn = new Button(this);
btn.setText("testBtn");
btn.setId(123456);
The problem is that the uiautomator, from the Android tools, is not recognize the id - I get empty id.
When I'm using XML file to add the button, all is working fine.
So my question is, what is the problem with the dynamically setting of the id, what are the changes between the two and why uiautomator isn't recognize the dynamically id?
P.S. Currently I prefer not the change all layouts to XML's
Upvotes: 4
Views: 1729
Reputation: 864
UIAutomator will not recognize dynamic ID's of widget which are set directly like in step1, instead we have to maintain xml file of unused ID's and use the unused ID in setID(), like in step2.
step1:
Button btn = new Button(this);
btn.setText("testBtn");
btn.setId(123456);
step2:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="newID" type="id"/>
</resources>
Button btn = new Button(this);
btn.setText("testBtn");
String packageName = getPackageName();
int resId = getResources().getIdentifier("newID", "id", packageName);
btn.setId(resId);
The above approach works cool.
Upvotes: 3
Reputation: 1693
It's an UIAutomator
limitiation.. UIAutomator
only detects the objects listed in XML layouts
Upvotes: 3