Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

View Hierarchy and Layout in Android

 

Now we will see what is the View Hierarchy in detail.

 

View

 

The View class is a superclass for all GUI components in Android. Android contains the following commonly used view subclasses :

 

Text View

Image Button

Edit Text

Progress Bar

Button

Checkbox

Image View

Date Picker

 

View Group

 

View Group instances work as containers for View instances to group view instances together.

 

Layouts in Android

 

The layout is again a very important part, everything we are using in our mobile application will have what kind of it's designing flow, we design through the layout. How will it look at a specific position on the screen, how one component will look related to the other component, so the graphical arrangement of all these is done with the help of layout.

 

File main_layout.xml which is located in the res/layout folder of your project.

 

Once your layout has created, you can load the layout resource from your application code, in your Activity.inCreate() callback implementation as shown below –

 

setContentView(R.layout.activity_main)

 

Android Layout Types 

 

1. Linear Layout in Android

A Linear Layout is a view group that aligns all children in a single direction, vertically or horizontally.

 

2. Relative Layout in Android

Suppose we want to have three buttons in our application that are of the same type such as 'Text view', then where will be the first Text view, and where will the second text view, above or below, in left or in right. We have to specify the particular position, which is done by relative layout.

 

3. Table Layout in Android

Table Layout is a view that group views into rows and columns.

 

4. Absolute Layout in Android

Absolute Layout enables you to specify the exact location of its children.

 

5. Frame Layout in Android

The Frame Layout is a placeholder on the screen that you can use to display a single view.

 

6. List View in Android

List View is a view group that displays a list of scrollable items.

 

7. Grid Layout in Android

It is a view group that displays a grid of scrollable items.

 

 

Layout Attributes in Android 

 

Attribute

Description

layout_width

Specifies the width of the view

layout_height

Specifies the height of the view

layout_marginTop

Specifies the extra space on the top side of the view

layout_marginBottom

Specifies the extra space on the bottom side of the view

layout_marginLeft

Specifies the extra space on the left side of the view

layout_marginRight

Specifies the extra space on the right side of the view

layout_gravity

Specifies how child views are positioned

layout_weight

Specifies how much of the extra space in the layout to be allocated to the view

layout_x

Specifies the x coordinate of the view

layout_y

Specifies the y coordinate of the view

 

View Identification

 

View Identification differentiates components from each other.

 

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”

 

Following is a brief description of @ and + signs.

Use the following –

Button myButton = (Button) findViewById(R.id.my_button);

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere