Creating A Layout Programmatically

The most common way to create a layout is by using an XML file, as you have seen in the examples in this chapter. However, it is also possible to create a layout programmatically, by instantiating the layout class and passing it to the addContentView method in an activity class. For instance, the following code is part of the onCreate method of an activity that programmatically creates a LinearLayout, sets a couple properties, and  passes it to addContentView.

LinearLayout root = new LinearLayout(this);

LinearLayout.LayoutParams matchParent = new






addContentView(root, matchParent);


A layout is responsible for arranging its child views. It directly affect the look and feel of an application. In this chapter you learned some of the layouts available in Android, LinearLayout, RelativeLayout, FrameLayout, TableLayout, and GridLayout.

Leave a Reply

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