Addev
Addev

Reputation: 32233

Get a layout inflater from the ApplicationContext

I'm following that guide for implementing a custom preference. http://android-journey.blogspot.com/2010/01/for-almost-any-application-we-need-to.html. In the method

@Override
protected View onCreateView(ViewGroup parent){
   ...
} 

The guide creates the elements programmatically and then returns the view. I'd like retrieve the layout from a xml file. But the call getLayoutInflater() is not accesible there how can I retrieve a layout inflater for get the view stored in the file "progressbarpreference.xml"?

I have a static reference to the application Context available if needed

Thanks

Upvotes: 6

Views: 7125

Answers (3)

atraudes
atraudes

Reputation: 2408

LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.yourcustomviewxml, layout);

Got this from a question I asked: How to pass AttributeSet to custom View Thanks Ian!

Upvotes: 2

Pradeep Sharma
Pradeep Sharma

Reputation: 134

Use the static reference, suppose if it is context.

rowLayout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.progress_bar, parent, false);

where progress_bar is the id of the linear layout.

Upvotes: 2

user658042
user658042

Reputation:

LayoutInflater li = (LayoutInflater) ctx.getSystemService(Service.LAYOUT_INFLATER_SERVICE);

should do the trick (ctx is your application context in this case).

Upvotes: 18

Related Questions