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
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!
Source: Stackoverflow.com