Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to getLayoutInflater() in places not in activity

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


Or ...

LayoutInflater inflater = LayoutInflater.from(context);

or

View.inflate(context, layout, parent)


Using context object you can get LayoutInflater from following code

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

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