SlideShare uma empresa Scribd logo
1 de 5
Chapter 4
Views and Layouts
By
Dr. Ramkumar Lakshminarayanan
Objectives
4.1. To understand the Views in Android Application
4.2. To understand the ViewGroup in Android Application
Introduction
One of the most important parts of any application's design and development is the
graphical user interface (GUI) and screen layout design. Android applications are popular
because of their visual design, animated graphics, and easy- or fun-to-use interfaces. We will
explore the Java classes that provide the core foundation for all of these front-end capabilities in
this chapter.
Android View
Two of the most important and often used classes in Android development:
 The View class
 The ViewGroup class
View and View Group are core, high-level classes, created or sub classed from the Java
Object class, as are all Java classes. View objects are created using the View class. The View
class can also be used to create many lower-level, or more customized, Java classes. Those
classes that are sub classed from the View class inherit the characteristics of their superclass.
So, the basic screen layout in Android is controlled by a View object, which contains a
complex data structure that represents the content and layout parameters for a given rectangular
section of the smartphone's display screen.
Using the View Class
There may be one or more View objects that make up the entire display screen.
Depending on UI structure of Android applications screen View and ViewGroup classes are
created.
Each View object controls and references its own rectangular view parameters, allowing
you to control many attributes. Here are just some examples of the many attributes controlled by
the View class parameters available to programmers:
 Bounds (measurements)
 Layout on the screen
 Order in which its layers are drawn
 Scrolling
 Focus
 Keystroke interactions
 Gesture interactions
Finally, Views have the ability to receive events—interaction events between the
application's end user and the View object itself. For this reason, the View class is the logical
Java construct to subclass to build more detailed and specific UI elements, such as buttons, check
boxes, radio buttons, and text fields.
The View class serves as the foundation for UI elements that are subclasses of the View
class. Recall that in Java, a subclass is a more specific or detailed implementation of the class
from which it is subclassed. For instance, the Button class is subclassed from the TextView class,
which is subclassed from the View class, which is subclassed from the Object class. The Button
class is subclassed from the TextView class because the Button has a TextView label and is thus
a more specialized version of a TextView; that is, it is a clickable TextView with a button
background appearance.
So many UI classes have been subclassed from the View class that there is a name for
them: widgets. All of these widgets are contained in a package (a collection of classes) called
android.widget. For example, Button class can be accessed via this package using
android.widget.button.
Using the ViewGroup Class for nesting views
One of the most useful classes subclassed from the View class is the ViewGroup class.
The ViewGroup class is used to subclass layout container classes, which allow groups of View
objects to be logically grouped, arranged, and cascaded onto the screen.
ViewGroups are layout containers, usually collections of UI elements. In the diagram in
Figure 4.1, View could mean a button, a text field, a check box, and so on. This applies to any
other type of UI element.
Figure 4.1 ViewGroups and nested Views and ViewGroups
Direct subclasses of the ViewGroup class include AbsoluteLayout, RelativeLayout,
FrameLayout, LinearLayout, and SlidingDrawer
In the diagram in Figure 4.1, the top level ViewGroup object is the parent of the View
objects and ViewGroup objects underneath it, which are called its children. The ViewGroup
object in the second row is both a child as well as a parent, and the same goes for the ViewGroup
object in the third row.
ViewGroup objects can contain other ViewGroup objects, but View objects cannot
contain other objects. They are the end object, and are simply UI components for which you can
set via a plethora of configuration parameters.
The primary way of defining screen layouts ( ViewGroup objects) is via XML. This
XML goes inside a file called main.xml, placed inside a folder called /res/layout within
application project folder.
Once this main.xml file is in place, with the XML screen layout (UI) definition inside it,
it can be used in the Java onCreate() method to push it onto the screen startup of the application
activity.
Setting Up for Your Screen Layout
Just three lines of Java code inside an onCreate() method set your content view to the
main.xml screen layout XML definition.
The words before the method name determine who can access its methods and data. A
public method is one that is open to any part of your Android application.
The words that follow the method name (always enclosed in parentheses) are the
parameters that an application can pass to the method for its use. Parameters are chunks of data
the method needs to do its job.
The savedInstanceState object is a Bundle object, which is a collection of all of the states
for your activity screen UI elements. It exists so that the screen UI elements can be restored if the
screen is replaced by navigation to other screens during the use of your application. The state of
a UI screen consists of its attributes and their values, including the UI elements it uses, which
one has the focus, the colors, and similar attributes that define its appearance.
The Activity class saves your state for you, by simply extending it, and it does the work
in the application.
The super keyword calls the superclass (the class containing the onCreate() method that
was subclassed from android.app.Activity), so it is basically referencing the onCreate() method
of the android.app.Activity class from which our activity class was subclassed. It's just a shortcut
for android.app.Activity.onCreate(savedInstanceState). Since it is called from this activity class,
it affects this activity locally and applies to this activity only. This savedInstanceState object is
the one Android kindly saves for us when it deals with saving state.
The onCreate() method will always be called by the Android operating system when any
activity (defined in the AndroidManifest.xml ) file is started. This part of the code is where all of
the initializations and UI definitions will be performed, so it must be present—at least the
application need for your users to interact with the smartphone screen area.
The way that layouts contain other nested layouts in XML code (as shown in Figure 4.1)
is by nesting them inside each other. The closing tags are nested at the bottom of these structures,
and they must be nested in the correct order to show Android which layouts are inside of which
other layouts. Layouts underneath or inside of another layout conform to, and are controlled by,
their parent layout container.
The most commonly used layout container in Android: the linear layout. The
LinearLayout class, which has been subclassed from the ViewGroup class, which is subclassed
from the View class, which is subclassed from the Object class.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
Chapter Summary
In this chapter we discussed about the View and View Groups. Android provides a
simple way to create layout files in XML as resources provided in the res/layout project
directory. This is the most common and convenient way to build Android user interfaces and is
especially useful for defining static screen elements and control properties. In the next chapter
we discuss about creating layout for android application.

Mais conteúdo relacionado

Mais procurados

Developing Dynamic PeopleSoft Field Security Applications:A PeopleSoft Develo...
Developing Dynamic PeopleSoft Field Security Applications:A PeopleSoft Develo...Developing Dynamic PeopleSoft Field Security Applications:A PeopleSoft Develo...
Developing Dynamic PeopleSoft Field Security Applications:A PeopleSoft Develo...
guest96f6c68d
 
Windows Programming with Swing
Windows Programming with SwingWindows Programming with Swing
Windows Programming with Swing
backdoor
 
Uml Interview Questions
Uml Interview QuestionsUml Interview Questions
Uml Interview Questions
Raj Chanchal
 

Mais procurados (10)

Android Bootcamp Tanzania:understanding ui in_android
Android Bootcamp Tanzania:understanding ui in_androidAndroid Bootcamp Tanzania:understanding ui in_android
Android Bootcamp Tanzania:understanding ui in_android
 
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & SpinnerAndroid User Interface Tutorial: DatePicker, TimePicker & Spinner
Android User Interface Tutorial: DatePicker, TimePicker & Spinner
 
Developing Dynamic PeopleSoft Field Security Applications:A PeopleSoft Develo...
Developing Dynamic PeopleSoft Field Security Applications:A PeopleSoft Develo...Developing Dynamic PeopleSoft Field Security Applications:A PeopleSoft Develo...
Developing Dynamic PeopleSoft Field Security Applications:A PeopleSoft Develo...
 
Windows Programming with Swing
Windows Programming with SwingWindows Programming with Swing
Windows Programming with Swing
 
Final report mobile shop
Final report   mobile shopFinal report   mobile shop
Final report mobile shop
 
Lab 3: Commenting on artifacts and customizing dashboards
Lab 3: Commenting on artifacts and customizing dashboardsLab 3: Commenting on artifacts and customizing dashboards
Lab 3: Commenting on artifacts and customizing dashboards
 
Vc++ 2
Vc++ 2Vc++ 2
Vc++ 2
 
Ui 5
Ui   5Ui   5
Ui 5
 
Vb tutorial
Vb tutorialVb tutorial
Vb tutorial
 
Uml Interview Questions
Uml Interview QuestionsUml Interview Questions
Uml Interview Questions
 

Destaque

Destaque (13)

Introduction to Android (in tamil)
Introduction to Android (in tamil)Introduction to Android (in tamil)
Introduction to Android (in tamil)
 
Android practice of layout in application-chapter6
Android practice of layout in application-chapter6Android practice of layout in application-chapter6
Android practice of layout in application-chapter6
 
Cloud performance tools
Cloud performance toolsCloud performance tools
Cloud performance tools
 
Android user interface design-chapter13
Android user interface design-chapter13Android user interface design-chapter13
Android user interface design-chapter13
 
Android building blocks and application life cycle-chapter3
Android building blocks and application life cycle-chapter3Android building blocks and application life cycle-chapter3
Android building blocks and application life cycle-chapter3
 
Android style resource and other resources types-chapter12
Android style resource and other resources types-chapter12Android style resource and other resources types-chapter12
Android style resource and other resources types-chapter12
 
Android Tips (Tamil)
Android Tips (Tamil)Android Tips (Tamil)
Android Tips (Tamil)
 
Android menus in android-chapter15
Android menus in android-chapter15Android menus in android-chapter15
Android menus in android-chapter15
 
Android Application using seekbar (in tamil)
Android Application using seekbar (in tamil)Android Application using seekbar (in tamil)
Android Application using seekbar (in tamil)
 
Creating Image Gallery - Android app (in tamil)
Creating Image Gallery - Android app (in tamil)Creating Image Gallery - Android app (in tamil)
Creating Image Gallery - Android app (in tamil)
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Rating Bar in Android Example
Rating Bar in Android ExampleRating Bar in Android Example
Rating Bar in Android Example
 
A Study of Computer Applications in the Management of Aavin Dairy Cooperative...
A Study of Computer Applications in the Management of Aavin Dairy Cooperative...A Study of Computer Applications in the Management of Aavin Dairy Cooperative...
A Study of Computer Applications in the Management of Aavin Dairy Cooperative...
 

Semelhante a Android views and layouts-chapter4

Mobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdfMobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdf
AbdullahMunir32
 
View groups containers
View groups containersView groups containers
View groups containers
Mani Selvaraj
 
Basic android development
Basic android developmentBasic android development
Basic android development
Upanya Singh
 
Basic android development
Basic android developmentBasic android development
Basic android development
Upanya Singh
 

Semelhante a Android views and layouts-chapter4 (20)

04 user interfaces
04 user interfaces04 user interfaces
04 user interfaces
 
Mobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdfMobile Application Development -Lecture 07 & 08.pdf
Mobile Application Development -Lecture 07 & 08.pdf
 
Building a simple user interface lesson2
Building a simple user interface lesson2Building a simple user interface lesson2
Building a simple user interface lesson2
 
View groups containers
View groups containersView groups containers
View groups containers
 
Android Study Jam 2
Android Study Jam 2Android Study Jam 2
Android Study Jam 2
 
Android Tutorial For Beginners Part-1
Android Tutorial For Beginners Part-1Android Tutorial For Beginners Part-1
Android Tutorial For Beginners Part-1
 
Hello Android
Hello AndroidHello Android
Hello Android
 
Android Training (Android UI)
Android Training (Android UI)Android Training (Android UI)
Android Training (Android UI)
 
UNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdfUNIT-2-AJAVA.pdf
UNIT-2-AJAVA.pdf
 
Notes Unit3.pptx
Notes Unit3.pptxNotes Unit3.pptx
Notes Unit3.pptx
 
mobile application development -unit-3-
mobile application development  -unit-3-mobile application development  -unit-3-
mobile application development -unit-3-
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
[PBO] Pertemuan 12 - Pemrograman Android
[PBO] Pertemuan 12 - Pemrograman Android[PBO] Pertemuan 12 - Pemrograman Android
[PBO] Pertemuan 12 - Pemrograman Android
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
"Android" mobilių programėlių kūrimo įvadas #2
"Android" mobilių programėlių kūrimo įvadas #2"Android" mobilių programėlių kūrimo įvadas #2
"Android" mobilių programėlių kūrimo įvadas #2
 
Interface Programming Android
Interface Programming AndroidInterface Programming Android
Interface Programming Android
 
Android xml-based layouts-chapter5
Android xml-based layouts-chapter5Android xml-based layouts-chapter5
Android xml-based layouts-chapter5
 
Basic android development
Basic android developmentBasic android development
Basic android development
 
Basic android development
Basic android developmentBasic android development
Basic android development
 

Mais de Dr. Ramkumar Lakshminarayanan

Mais de Dr. Ramkumar Lakshminarayanan (20)

IT security awareness
IT security awarenessIT security awareness
IT security awareness
 
Basics of IT security
Basics of IT securityBasics of IT security
Basics of IT security
 
IT Security Awareness Posters
IT Security Awareness PostersIT Security Awareness Posters
IT Security Awareness Posters
 
Normalisation revision
Normalisation revisionNormalisation revision
Normalisation revision
 
Windows mobile programming
Windows mobile programmingWindows mobile programming
Windows mobile programming
 
Web technology today
Web technology todayWeb technology today
Web technology today
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Phonegap for Android
Phonegap for AndroidPhonegap for Android
Phonegap for Android
 
Create and Sell Android App (in tamil)
Create and Sell Android App (in tamil)Create and Sell Android App (in tamil)
Create and Sell Android App (in tamil)
 
Android app - Creating Live Wallpaper (tamil)
Android app - Creating Live Wallpaper (tamil)Android app - Creating Live Wallpaper (tamil)
Android app - Creating Live Wallpaper (tamil)
 
Android Animation (in tamil)
Android Animation (in tamil)Android Animation (in tamil)
Android Animation (in tamil)
 
Creating List in Android App (in tamil)
Creating List in Android App (in tamil)Creating List in Android App (in tamil)
Creating List in Android App (in tamil)
 
Single Touch event view in Android (in tamil)
Single Touch event view in Android (in tamil)Single Touch event view in Android (in tamil)
Single Touch event view in Android (in tamil)
 
Create Android App using web view (in tamil)
Create Android App using web view (in tamil)Create Android App using web view (in tamil)
Create Android App using web view (in tamil)
 
Hardware Interface in Android (in tamil)
Hardware Interface in Android (in tamil)Hardware Interface in Android (in tamil)
Hardware Interface in Android (in tamil)
 
GPS in Android (in tamil)
GPS in Android (in tamil)GPS in Android (in tamil)
GPS in Android (in tamil)
 
Using many languages in single Android App (in tamil)
Using many languages in single Android App (in tamil)Using many languages in single Android App (in tamil)
Using many languages in single Android App (in tamil)
 
SQLite in Android App (in tamil)
SQLite in Android App (in tamil)SQLite in Android App (in tamil)
SQLite in Android App (in tamil)
 
Shared Preference in Android App
Shared Preference in Android AppShared Preference in Android App
Shared Preference in Android App
 
Data Storage in Android App
Data Storage in Android AppData Storage in Android App
Data Storage in Android App
 

Android views and layouts-chapter4

  • 1. Chapter 4 Views and Layouts By Dr. Ramkumar Lakshminarayanan Objectives 4.1. To understand the Views in Android Application 4.2. To understand the ViewGroup in Android Application Introduction One of the most important parts of any application's design and development is the graphical user interface (GUI) and screen layout design. Android applications are popular because of their visual design, animated graphics, and easy- or fun-to-use interfaces. We will explore the Java classes that provide the core foundation for all of these front-end capabilities in this chapter. Android View Two of the most important and often used classes in Android development:  The View class  The ViewGroup class View and View Group are core, high-level classes, created or sub classed from the Java Object class, as are all Java classes. View objects are created using the View class. The View class can also be used to create many lower-level, or more customized, Java classes. Those classes that are sub classed from the View class inherit the characteristics of their superclass. So, the basic screen layout in Android is controlled by a View object, which contains a complex data structure that represents the content and layout parameters for a given rectangular section of the smartphone's display screen. Using the View Class There may be one or more View objects that make up the entire display screen. Depending on UI structure of Android applications screen View and ViewGroup classes are created. Each View object controls and references its own rectangular view parameters, allowing you to control many attributes. Here are just some examples of the many attributes controlled by the View class parameters available to programmers:
  • 2.  Bounds (measurements)  Layout on the screen  Order in which its layers are drawn  Scrolling  Focus  Keystroke interactions  Gesture interactions Finally, Views have the ability to receive events—interaction events between the application's end user and the View object itself. For this reason, the View class is the logical Java construct to subclass to build more detailed and specific UI elements, such as buttons, check boxes, radio buttons, and text fields. The View class serves as the foundation for UI elements that are subclasses of the View class. Recall that in Java, a subclass is a more specific or detailed implementation of the class from which it is subclassed. For instance, the Button class is subclassed from the TextView class, which is subclassed from the View class, which is subclassed from the Object class. The Button class is subclassed from the TextView class because the Button has a TextView label and is thus a more specialized version of a TextView; that is, it is a clickable TextView with a button background appearance. So many UI classes have been subclassed from the View class that there is a name for them: widgets. All of these widgets are contained in a package (a collection of classes) called android.widget. For example, Button class can be accessed via this package using android.widget.button. Using the ViewGroup Class for nesting views One of the most useful classes subclassed from the View class is the ViewGroup class. The ViewGroup class is used to subclass layout container classes, which allow groups of View objects to be logically grouped, arranged, and cascaded onto the screen. ViewGroups are layout containers, usually collections of UI elements. In the diagram in Figure 4.1, View could mean a button, a text field, a check box, and so on. This applies to any other type of UI element.
  • 3. Figure 4.1 ViewGroups and nested Views and ViewGroups Direct subclasses of the ViewGroup class include AbsoluteLayout, RelativeLayout, FrameLayout, LinearLayout, and SlidingDrawer In the diagram in Figure 4.1, the top level ViewGroup object is the parent of the View objects and ViewGroup objects underneath it, which are called its children. The ViewGroup object in the second row is both a child as well as a parent, and the same goes for the ViewGroup object in the third row. ViewGroup objects can contain other ViewGroup objects, but View objects cannot contain other objects. They are the end object, and are simply UI components for which you can set via a plethora of configuration parameters. The primary way of defining screen layouts ( ViewGroup objects) is via XML. This XML goes inside a file called main.xml, placed inside a folder called /res/layout within application project folder. Once this main.xml file is in place, with the XML screen layout (UI) definition inside it, it can be used in the Java onCreate() method to push it onto the screen startup of the application activity. Setting Up for Your Screen Layout Just three lines of Java code inside an onCreate() method set your content view to the main.xml screen layout XML definition.
  • 4. The words before the method name determine who can access its methods and data. A public method is one that is open to any part of your Android application. The words that follow the method name (always enclosed in parentheses) are the parameters that an application can pass to the method for its use. Parameters are chunks of data the method needs to do its job. The savedInstanceState object is a Bundle object, which is a collection of all of the states for your activity screen UI elements. It exists so that the screen UI elements can be restored if the screen is replaced by navigation to other screens during the use of your application. The state of a UI screen consists of its attributes and their values, including the UI elements it uses, which one has the focus, the colors, and similar attributes that define its appearance. The Activity class saves your state for you, by simply extending it, and it does the work in the application. The super keyword calls the superclass (the class containing the onCreate() method that was subclassed from android.app.Activity), so it is basically referencing the onCreate() method of the android.app.Activity class from which our activity class was subclassed. It's just a shortcut for android.app.Activity.onCreate(savedInstanceState). Since it is called from this activity class, it affects this activity locally and applies to this activity only. This savedInstanceState object is the one Android kindly saves for us when it deals with saving state. The onCreate() method will always be called by the Android operating system when any activity (defined in the AndroidManifest.xml ) file is started. This part of the code is where all of the initializations and UI definitions will be performed, so it must be present—at least the application need for your users to interact with the smartphone screen area. The way that layouts contain other nested layouts in XML code (as shown in Figure 4.1) is by nesting them inside each other. The closing tags are nested at the bottom of these structures, and they must be nested in the correct order to show Android which layouts are inside of which other layouts. Layouts underneath or inside of another layout conform to, and are controlled by, their parent layout container. The most commonly used layout container in Android: the linear layout. The LinearLayout class, which has been subclassed from the ViewGroup class, which is subclassed from the View class, which is subclassed from the Object class. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }
  • 5. Chapter Summary In this chapter we discussed about the View and View Groups. Android provides a simple way to create layout files in XML as resources provided in the res/layout project directory. This is the most common and convenient way to build Android user interfaces and is especially useful for defining static screen elements and control properties. In the next chapter we discuss about creating layout for android application.