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:
Relative Layout:
Table Layout:
Absolute Layout:
Frame Layout:
List View
Grid View:
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);