ANDROID PROGRAMMING

User Interface Layout

 User Interface Layout

The building block for a user interface is View. It occupies a rectangular area on the screen and is responsible for drawing and event handling . View is the base class for widgets, which are used to create interactive User Interface components like buttons, text fields, etc. The ViewGroup is a subclass of View and provides invisible container that hold other Views or other ViewGroups and define their layout properties.
All the layouts are defined in .xml file written in xml coding.

Following is a simple example of XML file having LinearLayout:

Once our layout is defined, we loads them in our java file in the main activity in Activity.onCreate()callback implementation as shown below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

We have following types of layouts :-

Linear Layout:

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally.
Relative Layout:
RelativeLayout is a view group that displays child views in relative positions.
Table Layout:
TableLayout is a view that groups views into rows and columns.
Absolute Layout:
AbsoluteLayout enables you to specify the exact location of its children.
Frame Layout:
The FrameLayout is a placeholder on screen that you can use to display a single view.
List View 
ListView is a view group that displays a list of scrollable items.
Grid View:
GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.

Layout Attributes

There are few common attributes among all the layouts. Following are common attributes and will be applied to all the layouts


Button
android:id :- This is the ID which uniquely identifies the view.
android:layout_width :- This is the width of the layout.
android:layout_height :- This is the height of the layout

View Identification

A view object may have a unique ID assigned to it which will identify the View uniquely within the tree. The syntax for an ID, inside an XML tag is:
android:id=”@+id/my_button”
Here is a brief description of @ and + signs:
The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource.


The plus-symbol (+) means that this is a new resource name that must be created and added to our resources. To create an instance of the view object and capture it from the layout, use the following:
Button myButton = (Button) findViewById(R.id.my_button);

Leave a Reply

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

Back to top button