Android LayoutInflater Introduction

| Page Views: 269

Android LayoutInflater Tutorial and Example.

LayoutInflater is a class used to inflater XML layouts ino the android.view.View object. This is important since android applications primarily apply creation of user interfaces declaratively using XML(eXtensible Markup Language).

LayoutInflater Definition

  1. It's a public class hence can be used by other packages.
  2. It's an abstract class hence is never used directly.
public abstract class LayoutInflater{..}
  1. It resides in the android.view package since it's primary used to work with views.
package android.view;

Roles of LayoutInflater

  1. Inflate XML layouts into android.view.View object.

Obtaining a LayoutInflater instance

As an abstract class LayoutInflater is never used directly. Instead we use methods provided by either an activity or a context class. Here's how we retrieve those:

  1. Activity.getLayoutInflater
  2. Context.getLayoutInflater . This is the most commonly used way. It provides us with a LayoutInflater instance already hooked up to the current context and configured for the device we are running on.


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

Inflating a View using LayoutInflater

After obtaining the LayoutInflater instance we can the inflate a Layout in the following manner:

View myView=inflater.inflate(R.layout.custom_layout,parent,false);

How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section.

About Me.

After completing his Software Engineering bachelors program, Oclemy(Clement Ochieng) these days is a man of two lives. At day he works for a startup in Nairobi, Kenya. At night he works tirelessly on building ProgrammingWizards TV, a tv channel for student coders and this website to help share the source code. In between he practices Meditation and Self actualization to help him keep balance. He also likes going for long solo walks to connect more with nature.


What do You Think

Previous Post Next Post