Layout XML Code


The user interface for activities is defined via layouts, which define the UI elements, their properties and their arrangement. A layout tutorial can be found from here. A layout can be defined via Java code or via XML.
A Layout Using Java Code
You typically use Java code to generate the layout if you do not know the content until runtime; e.g., if your layout depends on content which you read from the Internet. An example of this is the next three statements whose screenshot is on the right:
   TextView tv = new TextView( this );
   tv.setText( "Hello, Android" );
   setContentView( tv );
which compose the string “Hello, Android” and display it.

A Layout Using XML
XML based layouts are defined via a resource file in the folder /res/layout. This file specifies the view groups, views, their relationship, and their attributes for a specific layout. Defining layouts via XML is usually the preferred way as this separates the programming logic from the layout definition. For example, the following two layouts are used in this application:


The application loads the screen specified in main.xml by the following Java command in the MainActivity.java:
   setContentView( R.layout.main );



      The story took on a life of its own and began    
      to appear on news broadcasts everywhere.