How to Get a Layout Inflater Given a Context?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Android, a LayoutInflater turns an XML layout resource into actual View objects at runtime. If all you have is a Context, the most direct and idiomatic way to obtain one is LayoutInflater.from(context), though the older getSystemService() route also works.
The Idiomatic Solution
The usual answer is:
This is preferred because it is short, readable, and expresses intent clearly. It also works with themed contexts, which matters when the inflated layout depends on the current theme.
You can then inflate a layout like this:
The third argument controls whether the inflated view is attached to the parent immediately. In adapters and many custom view patterns, false is the right choice.
The getSystemService() Alternative
The older approach asks the Context directly for the inflater service:
This works, but it is more verbose and easier to mistype. In day-to-day app code, LayoutInflater.from(context) is usually better.
Why Context Type Matters
Not all contexts are equally useful. An Activity context carries theme information for the current screen, while an application context may not reflect the same UI theme.
That matters when inflating:
- dialogs
- menus
- custom views
- themed rows in adapters
If a layout uses theme attributes, inflating it with the wrong context can lead to incorrect colors, styles, or missing resources. So the question is often not only "How do I get the inflater?" but also "Which context should provide it?"
A Real Example in a RecyclerView Adapter
Here, parent.context is usually the right source because it matches the view hierarchy the new row will join.
When You Already Have an Activity
If you are inside an Activity, you can also use:
That is effectively the same idea, just exposed as a convenience property on the activity. Still, if the only thing you have is a Context, use LayoutInflater.from(context).
Dialogs and Wrapped Contexts
There is one more practical case worth knowing: dialogs and other themed wrappers often use a context that is different from the base activity. Inflating with that wrapped context ensures the layout picks up the dialog theme, text appearances, and other visual overrides correctly. If the UI looks "almost right but not quite," the wrong inflation context is often the reason.
Common Pitfalls
The most common mistake is using the application context when a themed activity context is required. The code may run, but the inflated UI can look wrong.
Another issue is passing null as the parent too casually. Some layouts need parent layout parameters during inflation, even if you are not attaching the view immediately.
Developers also sometimes set attachToRoot to true in adapter code and then run into duplicate-attachment or layout problems. In many cases, false is the correct option for item views.
Finally, remember that getting the inflater is not the same as attaching the view. inflate() can create a view hierarchy without adding it to the screen right away.
Summary
- The idiomatic way to get a
LayoutInflaterfrom aContextisLayoutInflater.from(context). - '
getSystemService()also works, but it is more verbose.' - Use the right context, especially when theming matters.
- In adapters,
parent.contextis usually the best source. - Be deliberate about the
parentandattachToRootarguments during inflation.
Related reading
- How to get a list of installed android applications and pick one to run
- How to get a unique device ID in Swift?
- How to get a unique device ID in Swift?
- How to get Activity's content view?
- How to get AppBar height in Flutter
- How to get async call to return response to main thread, using okhttp?
- How to get Bitmap from an Uri?
- How to get Context in Android MVVM ViewModel
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.