What is the correct way to get layout inflater in Android?
Asked Answered
M

5

35

There is a way to get layoutInflater:

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

and another way is:

LayoutInflater inflater = LayoutInflater.from(context);

a third one (when I am in an Activity) is:

LayoutInflater inflater = getLayoutInflater();

So what is the difference between them?

Note that when I sent the third inflater to my adapter, my application worked. But when I sent the context and created the inflater via the second way, it didn't!

Malachite answered 8/1, 2014 at 11:45 Comment(5)
not much of a differencePneumonia
look for those methods on grepcode so you can understand the differences between themAfterworld
Aesthetically, 1st one :) Otherwise, all same.Vendue
see this developer.android.com/reference/android/view/…Supplant
Possible duplicate: https://mcmap.net/q/109661/-how-to-get-a-layout-inflater-given-a-context/187543Darmit
T
7

There is not much of a difference between them.

As doc says public abstract Object getSystemService (String name)

A LayoutInflater for inflating layout resources in this context.

And for the public static LayoutInflater from (Context context)

Obtains the LayoutInflater from the given context.

You can check this thread Is there any difference between getLayoutInflater() and .getSystemService(Context.LAYOUT_INFLATER_SERVICE)

Teleran answered 8/1, 2014 at 12:1 Comment(0)
R
20

use outside of your activity

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(
        Context.LAYOUT_INFLATER_SERVICE );

Within your activity

     LayoutInflater inflater = getLayoutInflater();

Check this

If you open up the Android source you can see that the LayoutInflator.from method looks like so:

    /**
     * Obtains the LayoutInflater from the given context.
     */
    public static LayoutInflater from(Context context) {
        LayoutInflater LayoutInflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (LayoutInflater == null) {
            throw new AssertionError("LayoutInflater not found.");
        }
        return LayoutInflater;
    }

and there is no difference

As long as the Activity or Window that calls getLayoutInflater() has the same Context that would call getSystemService(), there is no difference.

Rimarimas answered 8/1, 2014 at 12:22 Comment(1)
you made y day, i was searching from last five days but did not get luck with this error, now your solution works fine @Rimarimas DeoreStaccato
T
7

There is not much of a difference between them.

As doc says public abstract Object getSystemService (String name)

A LayoutInflater for inflating layout resources in this context.

And for the public static LayoutInflater from (Context context)

Obtains the LayoutInflater from the given context.

You can check this thread Is there any difference between getLayoutInflater() and .getSystemService(Context.LAYOUT_INFLATER_SERVICE)

Teleran answered 8/1, 2014 at 12:1 Comment(0)
A
5

The only difference is the context that you use. If the context that you use with LayoutInflater.fromContext() or context.getSystemService(...) is actually an Activity, it should be equivalent to Activity.getLayoutInflater(). If it's the application object, you might have problems inflating views that contain fragments, IIRC.

Ambler answered 8/1, 2014 at 12:11 Comment(0)
V
3

Actually I think that the getLayoutInflater() - Method of Activity is a convenience - method.

Remember that Activity subclasses Context, so all of the Methods available within Context are also available in the Activity Class.

Internally there will be a call to LayoutInflater.fromContext() or context.getSystemService(), so I would stick to context.getSystemService both to avoid the unnecessary method call as well to clarify that I am making a call to a system service.

Vigilant answered 8/1, 2014 at 12:34 Comment(0)
D
-2

*import android.content.Context;

just import this class and it will be work

* LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

Dionnadionne answered 6/5 at 16:46 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Supersede

© 2022 - 2024 — McMap. All rights reserved.