Skip to content

Instantly share code, notes, and snippets.

@girish3
Last active November 11, 2018 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save girish3/cb5843b73d4c5eb941cf8077a6f7e813 to your computer and use it in GitHub Desktop.
Save girish3/cb5843b73d4c5eb941cf8077a6f7e813 to your computer and use it in GitHub Desktop.
[Layout Inflater] Ways to get layout inflator object and inflate the view. #android_snippet #android
// if you are in an activity
LayoutInflater inflater = getLayoutInflater();
// If you have the context
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// or in a cleaner way, from() uses system service interally so its the same inflater
LayoutInflater inflater = LayoutInflater.from(context);
// inflating a view
// if you need to attach the inflated to rootView, returned view will be rootView.
// 3rd parameter is attach_to_root
inflater.inflate(R.layout.my_layout, rootView, true);
// or if not, returned view will be the inflated view.
inflater.inflate(R.layout.my_layout, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment