Inflate Android Layout Using Layout Inflater Service

Below is a small snippet in Android Java code to inflate a layout xml file into as a View using a Layout Inflater Service. Very useful if you intend to insert or include (inflate) any layout into another layout during runtime.

AbsoluteLayout mainLayout = (AbsoluteLayout) findViewById(R.id.your_main_layout);
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);

Assuming that your current activity has an AbsoluteLayout container named mainLayout in which a menuLayout is going to be inflated inside of it on the fly (during runtime) as a View. We use a LayoutInflater to inflate another layout file into another layout.

The loaded xml layout file is your_menu_layout.xml inside your project’s resources/layout directory.

2 Replies to “Inflate Android Layout Using Layout Inflater Service”

Leave a Reply

Your email address will not be published. Required fields are marked *

*

CAPTCHA Image

*