Wednesday, June 25, 2014

Android Layouts - Details

Android Layouts 

A layout defines visual structure for a user interface such as the UI for an activity or app widget. 

There are two ways we can define a layout. 

1. Declare UI in XML 
2. Instantiate Layout elements at runtime : Application can create view and ViewGroup objects (and manipulate their properties) at runtime. 

Android provides flexibility for either of these or a combination of both as well. 

A sample layout XML file is 

android:layout_width = "fill_parent"
android:layout_height="fill_parent"
android:orientation = "vertical" >
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:text = "Hello, am a TextView"
/>
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:text = "Hello, am a button"/>

Once after the layout is defined, we need to save it in the "/res/layout" directory.

Android system will compile each of the layout file to a View resource. The view resource should be loaded in Application code from the Activity.onCreate method. 
setContentView(R.layout.main_layout); as an example. 

In order to create views and refer from the part of the application, the common pattern is 

No comments:

Post a Comment