Call getLayoutInflater() in places not in activity
Solution 1
You can use this outside activities - all you need is to provide a Context
:
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
Then to retrieve your different widgets, you inflate a layout:
View view = inflater.inflate( R.layout.myNewInflatedLayout, null );
Button myButton = (Button) view.findViewById( R.id.myButton );
EDIT as of July 2014
Davide's answer on how to get the LayoutInflater
is actually more correct than mine (which is still valid though).
Solution 2
Or ...
LayoutInflater inflater = LayoutInflater.from(context);
Solution 3
or
View.inflate(context, layout, parent)
Solution 4
Using context object you can get LayoutInflater from following code
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Solution 5
LayoutInflater.from(context).inflate(R.layout.row_payment_gateway_item, null);
Related videos on Youtube
Comments
-
Lukap about 2 years
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 ?
-
Lukap over 12 yearsGreat, But now the findViewById doesn't work, do you have any ideas about that ? inflater.inflate(R.layout.some_layout, (ViewGroup) findViewById(R.id.parent));
-
Lukap over 12 yearsnop, the inflater.inflate() method doen't have overloaded method with just one int param, but I guess the next one could be null.
-
Raanan over 10 yearsVery late, but actually a better answer as the from function also checks with assert that you actually get an inflater back and throws an error otherwise - which will be much easier to deal with then a null pointer excpetion somewhere in the code. grepcode.com/file/repository.grepcode.com/java/ext/…
-
Danyal Aytekin over 10 yearsSo brief, much inflater
-
DenisGL about 8 yearsThat's fine ; however, with this method, I cannot provide the boolean "attachToRoot"
-
Prakash Nadar about 8 yearsIt is based on the requirement, if you do not need attachToRoot then this is a convenient helper method or pass getRootView() as the parent to the method.
-
Rohan Bhatia over 6 years@kaspermoerch why did you say David's answer is more correct?
-
kaspermoerch over 6 years@RohanBhatia Davides answer does not require casting which mine does. If the call to
getSystemService
for some (unlikely) reason does not return an object of typeLayoutInflater
then my code would cause a runtime exception. -
HansHirse about 5 yearsYou're encouraged to provide some explanatory text along with your code.
-
CodeFluid about 5 yearsWorked for me. Thanks
-
Tamás Bolvári almost 5 years@kaspermoerch Does this quote mean that your solution is better than Davide's? "It is never used directly. Instead, use getLayoutInflater() or getSystemService(Class ) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on." developer.android.com/reference/android/view/LayoutInflater
-
Tamás Bolvári almost 5 years@Davide Does this quote mean that kaspermoerch's solution is better than yours? "It is never used directly. Instead, use getLayoutInflater() or getSystemService(Class ) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on." developer.android.com/reference/android/view/LayoutInflater
-
Davide almost 5 years@TamásBolvári Is only more easy and clear, in fact the internal implementation is the same as Kaspermoerch write.
-
kaspermoerch almost 5 years@TamásBolvári No. Both solutions are equaly valid. Davide's solution is simpler than mine and you avoid casting the result to LayoutInflater which could potentially fail (although it is unlikely).