4

I have the following View structure in my activity:

  • Relative Layout (id = 1)
    • Relative Layout (id = 104)
      • TextView (id = 200)

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:

  1. Log.d ("demo", "Inside text_view parent.parent.id: " + (v.parent.parent as ViewGroup).id)
  2. 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

1 Answers1

0

Please check what view you have got exactly by below code:

Log.d ("tally", "Inside text_view parent: " + (v.parent).toString())
Log.d ("tally", "Inside text_view parent.parent.id: " + (v. parent.parent).toString())

Because, I think, v.rootView give you topmost view of view hierarchy .

[UPDATE]

After our threads of Comment, I update here, how to track, what is exactly you got after calling v.rootView

first, read another link to better understand android's view hierarchy

https://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:

Log.d ("demo", "Inside text_view parent: " + (v.parent).toString())
Log.d ("demo", "Inside text_view parent.parent.id: " + (v.parent.parent).toString())

Log.d ("demo", "Inside text_view getRootView: " + (v.getRootView()).toString())
Log.d ("demo", "Inside text_view getRootView Name: " + (v.getRootView().accessibilityClassName).toString())
Log.d ("demo", "Inside text_view getRootView is ViewGroup: " + (v.getRootView() is ViewGroup))

if(v.getRootView() is ViewGroup){
  var vg: ViewGroup  = v.getRootView() as ViewGroup;
  trackParentView(v,0, vg)
}

Now, use below method trackParentView(...):

fun trackParentView(view :View, cnt :Int, rootView :ViewGroup){
   //Log.d ("trackParentView()", "view (name) [track: "+cnt+"]: "+ view.accessibilityClassName)
   Log.d ("trackParentView()", "view (toString) [track: "+cnt+"]: "+ view.toString())
   if(view.parent != rootView){
     trackParentView(view.parent as View, cnt+1, rootView)
   } else {
     Log.d ("trackParentView()", "this is rootView at : ["+cnt+"]: "+ view.toString())
   }
 }
  • Inside text_view parent: android.widget.RelativeLayout{ace2ed7 V.E...... ........ 0,0-1080,300 #68} Inside text_view parent.parent.id: android.widget.RelativeLayout{d59a3c4 V.E...... ........ 0,0-1080,2075 #1}, I'm getting the Two Relative Layout (parent layout and parent's parent layout) – Jatin guglani Jul 10 '23 at 09:13
  • Now, check `Log.d ("tally", "Inside text_view rootViewID: " + (v.rootView).toString())`. is it give `RelativeLayout` or not? – Pravin Tadvi Jul 10 '23 at 09:15
  • Inside text_view rootViewID: DecorView@ca8a2ad[MainActivity], It's not giving me a RelativeLayout, but i don't understand why so and what is DecorView – Jatin guglani Jul 10 '23 at 09:20
  • Yes, my point is `v.rootView` only give you `topmost view of your view hierarchy`, so you can not use that. `DecorView` is `the root of the window's view hierarchy.` ref link: https://developer.android.com/reference/android/view/View#getRootView() – Pravin Tadvi Jul 10 '23 at 09:31
  • Oh, sorry for put wrong ref link at above comment, please see this link to understand `what is decorview`? https://akashsaggu-55383.medium.com/overlay-using-decorview-phonewindow-f13708710475 – Pravin Tadvi Jul 10 '23 at 09:49
  • Please see my updated answer to `track` `v.rootView` and understand `what actually you got` after call `View.rootView`. – Pravin Tadvi Jul 10 '23 at 12:18
  • Thanks for the info you have provided, got a lot of clarity on Android's view hierarchy. Now what I want to ask is if I have a TextView(id = 200) inside a RelativeLayout (id = 104) which itself inside a RelativeLayout (id = 1), can I get the id of my topmost relative layout (id =1) directly using the TextView or I have to do (textview.parent.parent as ViewGroup).id, because later I can have multiple numbers of relative layouts in hierarchy and a textview at last so I want to avoid doing textview.parent.parent.parent... n number of times. to get the top relative layout's id. – Jatin guglani Jul 13 '23 at 08:58
  • `can I get the id of my topmost relative layout (id =1) directly using the TextView.` Yes, for that you can use my method that describe in updated answer named `trackParentView`. For your result, just modify into my method, change condition `if(view.parent != relativeLayout)` and get `id` from that `view`. – Pravin Tadvi Jul 14 '23 at 10:30