210

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 ?

Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38
Lukap
  • 31,523
  • 64
  • 157
  • 244

6 Answers6

440

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).

Community
  • 1
  • 1
kaspermoerch
  • 16,127
  • 4
  • 44
  • 67
  • Great, 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 Oct 18 '11 at 07:48
  • nop, the inflater.inflate() method doen't have overloaded method with just one int param, but I guess the next one could be null. – Lukap Oct 18 '11 at 08:06
  • 1
    @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 type `LayoutInflater` then my code would cause a runtime exception. – kaspermoerch Jan 02 '18 at 14:46
  • @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." https://developer.android.com/reference/android/view/LayoutInflater – Tamás Bolvári Jul 12 '19 at 16:12
  • @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). – kaspermoerch Aug 12 '19 at 08:03
296

Or ...

LayoutInflater inflater = LayoutInflater.from(context);
Davide
  • 3,407
  • 1
  • 20
  • 22
  • 5
    Very 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. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.2_r1.1/android/view/LayoutInflater.java#LayoutInflater.from%28android.content.Context%29 – Raanan Nov 05 '13 at 23:45
  • @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." https://developer.android.com/reference/android/view/LayoutInflater – Tamás Bolvári Jul 12 '19 at 16:13
  • @TamásBolvári Is only more easy and clear, in fact the internal implementation is the same as Kaspermoerch write. – Davide Jul 15 '19 at 09:56
12

or

View.inflate(context, layout, parent)

Prakash Nadar
  • 2,694
  • 1
  • 19
  • 20
  • That's fine ; however, with this method, I cannot provide the boolean "attachToRoot" – DenisGL Mar 20 '16 at 08:06
  • It 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. – Prakash Nadar Mar 20 '16 at 16:26
11

Using context object you can get LayoutInflater from following code

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
anujprashar
  • 6,263
  • 7
  • 52
  • 86
4
LayoutInflater.from(context).inflate(R.layout.row_payment_gateway_item, null);
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Sikander Bakht
  • 239
  • 2
  • 8
3
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

Use this instead!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129