[android] Call to getLayoutInflater() in places not in activity

What does need to be imported or how can I call the Layout inflater in places other than activity?

public static void method(Context context){
    //this doesn't work the getLayoutInflater method could not be found
    LayoutInflater inflater = getLayoutInflater();
    // this also doesn't work 
    LayoutInflater inflater = context.getLayoutInflater();
}

I am able to call getLayoutInflater only in activity, is that an restriction? What if I want to create custom dialog and I want to inflate view for it, or what if I want to have Toast message with custom view that is shown from a service, I only have the context from the service I do not have any activity but I want to show custom message.

I need the inflater in places in the code that isn't in the activity class.

How can I do this ?

This question is related to android service android-context toast layout-inflater

The answer is


LayoutInflater.from(context).inflate(R.layout.row_payment_gateway_item, null);

Using context object you can get LayoutInflater from following code

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

or

View.inflate(context, layout, parent)


Or ...

LayoutInflater inflater = LayoutInflater.from(context);

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

Use this instead!


Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to service

Starting ssh-agent on Windows 10 fails: "unable to start ssh-agent service, error :1058" startForeground fail after upgrade to Android 8.1 Context.startForegroundService() did not then call Service.startForeground() How to restart a windows service using Task Scheduler the MySQL service on local computer started and then stopped Android: keep Service running when app is killed How to create a service running a .exe file on Windows 2012 Server? Start systemd service after specific service? Spring Boot application as a Service SSRS expression to format two decimal places does not show zeros

Examples related to android-context

How to get a context in a recycler view adapter get Context in non-Activity class android - How to get view from context? How to get my activity context? What's "tools:context" in Android layout files? Difference between getContext() , getApplicationContext() , getBaseContext() and "this" How do I view Android application specific cache? How to call getResources() from a class which has no context? Using context in a fragment How do you obtain a Drawable object from a resource id in android package?

Examples related to toast

How to create Toast in Flutter? java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare(); Custom toast on Android: a simple example Use Toast inside Fragment Call to getLayoutInflater() in places not in activity How to display Toast in Android? How to change position of Toast in Android?

Examples related to layout-inflater

onCreateOptionsMenu inside Fragments How does the getView() method work when creating your own custom adapter? Call to getLayoutInflater() in places not in activity How to change the Text color of Menu item in Android? What does LayoutInflater in Android do? How to change the background color of the options menu? How to inflate one view with a layout