I am running into the findViewById()
returning NULL problem that a lot of them seem to encounter. I understand that the findViewById() should only be called after the View is inflated in onFinishInflate()
. My question is - what is view inflation?
In the Activity I call setContentView(R.layout.testview)
which should call the View constructor and also calls the onDraw(). So that inflates the view does it not? If so why is there a LayoutInflater
and a function to actually inflate the view? In fact in my code onInflateView()
is never called even though the whole view is rendered and I was able to interact with the program. That tells me that View inflation is somehow different from calling the View's constructor and onDraw()
functions. Can someone explain please?
EDIT: Also I have a custom view that I draw using the onDraw() function. Somehow the onFinishInflate() function is never called for me. What can be the reason for this?
-P
LayoutInflater is a class used to instantiate layout XML file into its corresponding view objects which can be used in Java programs. In simple terms, there are two ways to create UI in android. One is a static way and another is dynamic or programmatically.
FindViewById(Int32)Finds a view that was identified by the android:id XML attribute that was processed in #onCreate .
attachToRoot: attaches the views to their parent (includes them in the parent hierarchy), so any touch event that the views recieve will also be transfered to parent view. Now it's upto the parent whether it wants to entertain those events or ignore them.
My question is - what is view inflation?
View inflation is the act of converting a layout XML file into the corresponding tree of View
objects. You manually do this using LayoutInflater
, or Android does it for you from setContentView()
, built-in Adapter
classes, etc.
In the Activity I call setContentView(R.layout.testview) which should call the View constructor and also calls the onDraw(). So that inflates the view does it not?
setContentView()
inflates the supplied layout file.
If so why is there a LayoutInflater and a function to actually inflate the view?
Because sometimes you need to do it by hand, such as in a complex ListView
with different types of rows (e.g.,. header and detail).
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