Call getLayoutInflater in places not in activity
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
getLayoutInflater() is an Activity convenience method, not a general Android API you can call from anywhere. Outside an activity, the real solution is to obtain a LayoutInflater from a Context, use the inflater already passed into a callback, or rely on fragment-specific lifecycle methods that expose one for you.
Use LayoutInflater.from(context)
If you are in a class that has access to a valid Context, use LayoutInflater.from(context):
This is the standard replacement for getLayoutInflater() outside an activity.
Common places where this works:
- custom views
- adapters
- helper classes that receive an activity or themed context
- dialog builders
The important part is the quality of the Context. A theme-aware UI context is usually better than the application context when inflating visual layouts.
In Fragments, Use the Inflater You Already Get
Fragments do not need getLayoutInflater() from an activity most of the time, because onCreateView() already receives a LayoutInflater:
That is the cleanest option because it uses the inflater provided for the fragment lifecycle. If you later need an inflater elsewhere inside the fragment, LayoutInflater.from(requireContext()) is also valid.
In Adapters, Inflate from the Parent Context
RecyclerView and ListView adapters are another common place where developers try to call getLayoutInflater(). Use the parent view group instead:
Using parent.context is usually better than holding a separate inflater field because it preserves the correct themed context for that part of the hierarchy.
Dialogs and Other Non-Activity Classes
When you need to inflate a custom dialog view, the same principle applies:
You do not need direct access to Activity.getLayoutInflater() to build the UI. You only need the right context and the correct parent or attachment behavior.
In helper classes, it is usually cleaner to pass the needed Context from the caller and create the inflater at the point of use. That avoids storing stale inflater references and keeps layout creation aligned with the current themed environment.
Common Pitfalls
The biggest mistake is using the application context for inflating themed UI. The view may inflate, but it can miss activity-level theme attributes, leading to incorrect colors, text appearances, or material components behavior.
Another common issue is inflating with the wrong parent arguments. Passing null everywhere works in some cases, but layout parameters from the intended parent can be lost. In adapters, inflate(layout, parent, false) is usually the correct form.
Developers also often ask for getLayoutInflater() in services. Most service code should not inflate interactive UI at all. If you truly need a view, make sure the surrounding component and windowing model support it.
Finally, do not treat getLayoutInflater() as special magic. It is mainly a convenience wrapper around the same inflater mechanism available through LayoutInflater.from(context).
Summary
- '
getLayoutInflater()is an activity convenience, not the only way to inflate layouts.' - Outside activities, use
LayoutInflater.from(context). - In fragments, use the
inflaterpassed toonCreateView()when possible. - In adapters, prefer
LayoutInflater.from(parent.context). - Use a theme-aware context and the correct parent arguments to avoid broken UI styling or layout params.

