0

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

Osin94
  • 11
  • 1

1 Answers1

0

In simple words: LayoutInflater helps you to "exchange" layout written in XML to code, its kind of parser which take XML input and creates proper Views using Context (some simple way of new View object creation in first code line in your question).

Activity does that under the hood giving us devs simple setContentView and findViewById methods, but you may also want to add some custom View defined in XML additionally (because some if) or doring runtime, then you can use LayoutInflater based on same Context which views already created/shown are using (e.g. Activity itself).

Note that when you use setContentView then LayoutInflater will attach some XML-based Views to Activity, so further line

view = findViewById(R.id.textView)

will return reference to already inflated existing View, meanwhile

view = new View(context)

is just creating some dummy View in memory using context, it isn't added to Activity, Fragment, any shown ViewGroup, thus is not even visible (and its not same view instance than one found with findViewById).

Darshan
  • 4,020
  • 2
  • 18
  • 49
snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • these are not same views, first one is temporary, not visible, second one, if not null (so added) will return some view from earlier inflated `R.layout` file, doen by `LayoutInflater` also, but by `Activity` itself under the hood. note `Fragment`s have some different API, more GUI-oriented, in there you should use inflater in `onCreateView` – snachmsm Jan 12 '23 at 14:36
  • just think about it like about XML parser: takes first line of text, ok its `LineatLayout`, so it creates `new LinearLayout(context)`, then next item in XML is `TextView` inside `LinearLayout` tag, so inflater creates `new TextView(context)`, sets its attributes from XML (probably every xml param has representation in code setting), and at the end `firstLinearLayout.addView(textView)`. at the end inflater returns built `View` which is probably an instance of `ViewGroup` - main holder of all your views in XML – snachmsm Jan 12 '23 at 14:39
  • if you want to know what `new View(...)` does - thats creation of object, memory allocation etc. Note `new` is keyword of Java, thats "feature" of language (object creation, not every lang is object-oriented), not even Android-only. `findViewById` method of particular framework - Android - and is getting reference to obejct which was also created in similar way, but "autmatically" by inflater basing on given XML – snachmsm Jan 12 '23 at 14:41
  • thats not so kind to remove comments, mine are now out of topic... – snachmsm Jan 12 '23 at 14:44
  • Thumbs up for you, thanks. I see lack of my knowledge in many terms you using. I will try to learn more about this term and maybe later i will understand this, for know i will be using this as a template :) Thanks again – Osin94 Jan 12 '23 at 14:46
  • for now, at the begin of your Android journey, focus on `findViewById`, also get familiar with `Fragment`. further inspect `Compose`, thats pretty new way, XML-less. if you want to stick with XMLs read about "layout binding", il will do even more work for you it that topic making `Activity` code cleaner – snachmsm Jan 12 '23 at 14:51
  • if you found my answer and comments helpful then think about upvoting and/or accepting my answer :) good luck! – snachmsm Jan 12 '23 at 14:51
  • I can't upvoting cous of lack reputation but I accept your anwser at least I can do this. Thanks again, best wishes. – Osin94 Jan 12 '23 at 14:56