What is the difference between findViewById(R.id.content) and getRootView() ? Don't both return the root view of an activity?
FindViewById(Int32)Finds a view that was identified by the android:id XML attribute that was processed in #onCreate .
The android. R. id. content ID value indicates the ViewGroup of the entire content area of an Activity .
findViewById returns an instance of View , which is then cast to the target class. All good so far. To setup the view, findViewById constructs an AttributeSet from the parameters in the associated XML declaration which it passes to the constructor of View . We then cast the View instance to Button .
The findViewById() method is a method of Android's View and Activity classes. The method is used to find an existing view in your XML layout by its android:id attribute.
The method findViewById(R.id.content)
searches down the view hierarchy for a View with the id content
(it examines itself as well).
The method getRootView()
walks up view hierarchy until it hits the root View (it examines itself as well).
If the View you are in has the id content
and it is the root view then these methods will give the same result, otherwise they won't.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With