I've read one of answers here, and I am still confused.
""When I first started Android programming, I was really confused by LayoutInflater and findViewById. Sometimes we used one and sometimes the other.
LayoutInflater is used to create a new View (or Layout) object from one of your xml layouts.
findViewById just gives you a reference to a view than has already been created. You might think that you haven't created any views yet, but whenever you call setContentView in onCreate, the activity's layout along with its subviews gets inflated (created) behind the scenes."
I think I understand so when LayoutInflater creat a new object View, why we can't just do this:
view = new View(context);
view = findViewById(R.id.textView);
I missed something ? Thank you for anwsere.
I tried to undestrand way of working LayOutInflater
In simple words:
LayoutInflaterhelps you to "exchange" layout written in XML to code, its kind of parser which take XML input and creates properViews usingContext(some simple way ofnew Viewobject creation in first code line in your question).Activitydoes that under the hood giving us devs simplesetContentViewandfindViewByIdmethods, but you may also want to add some customViewdefined in XML additionally (because someif) or doring runtime, then you can useLayoutInflaterbased on sameContextwhich views already created/shown are using (e.g.Activityitself).Note that when you use
setContentViewthenLayoutInflaterwill attach some XML-basedViews toActivity, so further linewill return reference to already inflated existing
View, meanwhileis just creating some dummy
Viewin memory using context, it isn't added toActivity,Fragment, any shownViewGroup, thus is not even visible (and its not sameviewinstance than one found withfindViewById).