I have the following View structure in my activity:
- Relative Layout (id = 1)
- Relative Layout (id = 104)
- TextView (id = 200)
- Relative Layout (id = 104)
Now when TextView is touched, I want to get the topmost relative layout's id (i.e. id = 1)
I'm using setOnTouchListener and inside that, I'm writing two log statements as follows:
- Log.d ("demo", "Inside text_view parent.parent.id: " + (v.parent.parent as ViewGroup).id)
- Log.d ("demo", "Inside text_view rootViewID: " + (v.rootView as ViewGroup).id)
I'm able to get my topmost relative layout's id (id = 1) using 1st log statement but the second log statement is printing -1.
could someone please explain why I'm not able to get the topmost relative layout using View.rootView?
for reference: View.rootView
Please check what view you have got exactly by below code:
Because, I think,
v.rootViewgive youtopmost viewofview hierarchy.[UPDATE]
After our threads of Comment, I update here, how to track, what is exactly you got after calling
v.rootViewfirst, read another link to better understand
android's view hierarchyhttps://medium.com/@MrAndroid/android-window-basic-concepts-a11d6fcaaf3f#:~:text=DecorView%20is%20a%20ViewGroup%20so,event%20in%20the%20dispatchTouchEvent%20method.
Now, let's try below things:
Now, use below method
trackParentView(...):