I am looking for a way to give every of my composa...
# compose
d
I am looking for a way to give every of my composables a specific id related on the view hierarchy. Unfortunately, there is no real hierarchy in compose and I don't want to pass these ids with parameters in the composable functions. I read something about the semantics tree in this promising article , but they use a class "_AndroidComposeView_" which has a property semanticOwner. However I don't know how to get a working instance of this class to deal with. It does not seem to have anything to do with the normal ComposeView which is created when using setContent in a fragment. Any ideas?
z
there is no real hierarchy in compose
There is definitely a real hierarchy in Compose, in fact there are multiple: Composables, LayoutNodes, Modifiers, and Semantics to name a few. This is why giving a "composable" an ID is tricky – a call to a composable function may represent nodes in 0 or more of these trees.
Why are you trying to specify IDs? What are you trying to use them for?
1
👆 1
One thing to note is that test tags are already exposed through android's
AccessibilityNodeInfo
.
d
It is for assigning test IDs to the composables so that they are filled automatically in accordance to the on screen where they appear. Example: On screen "Song list" I want to have the 10th item in the list automatically with the id "songList_item10" so that they inherit the id of the parent item. The same composable should get the id "pictureList_item10" in a different screen "picture_list". Of course with more flexibility if there are more parents. I want to achieve this without passing the parent IDs as parameters.
z
You should be able to use test tags for that, you can use Compose's test library to access them through semantics, or if you are integrating with views you could access the
AccessibilityNodeInfo
tree yourself on API 34+.