SlideShare a Scribd company logo
1 of 23
Android Custom Views
• Name: Babar Sana
• Department: Android
• Designation: Software Engineer
Table of Contents
1.Custom Views
• Default views
• How Android Draws views
• Reason for creating views
• Responsibility of views
• Using new views in your layout files
• Create Screenshot of Views
Table of Contents
2.Compound Views
3.Creating Custom Views
• Creating Custom Views
• Measurements
• Defining Custom layout manager
4.Life Cycle
•Life Cycle Events related to Windows
•Traversal life cycle Events
•Activity Life Cycle
5.Define Additional Attributes
6.Question
Table of Contents
1.Custom Views
The Android framework provides several default views but developer can also create
their custom views and use them in their application. The base class a view is the View.
How Android Draws Views
• Once an Activity receives the focus, it must provide the root node of its layout
hierarchy to the Android system. Afterwards the Android system starts the drawing
procedure.
• Drawing begins with the root node of the layout. The layout hierarchy is traversed
in the order of declaration, i.e., parents are drawn before their children and children
are drawn in the order of declaration.
• Drawing the layout is a two pass process:
• measuring pass - implemented in the measure(int, int) method and is a top-down traversal
of the view hierarchy. Every view stores its measurements.
• layout pass - implemented in the layout(int, int, int, int) method is also a top-down traversal
of the view hierarchy. During this phase each layout manager is responsible for positioning all
of its children. It uses the the sizes computed in the measure pass.
How Android Draws Views
• A view or activity can retrigger the measure and layout pass with a call to the
requestLayout() method.
• After the measure and layout calculation, the views draw themselves. This
operation can be triggered with the invalidate() method from the View class.
Reasons for Creating Views
• View are typically created to provide a user interface experience with is not
possible with the default views.
• Using custom view allows the developer allow to do certain performance
optimization
Responsibility of Views
• Views are responsible for measuring, layouting and drawing themselves and their
child elements (in case of a ViewGroup)
• Views are also responsible for saving their UI state and handling touch events
Ways of creating custom views
• Customs views are typically classified as compound views or custom
views. It is possible to create custom views by:
• Compound views
• Custom Views
– By extending an existing view
• By extending the View class
Using new views in layout files
• Custom and compound views can be used in layout files. For this you need
to use the full qualified name in the layout file, e.g. using the package and
class name.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<de.android.ownview.MyDrawView
android:id="@+id/myDrawView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Create screenshots (Images) of views
• Every View class support the creating of an image of its current display.
The following coding shows an example for that.
# Build the Drawing Cache
view.buildDrawingCache();
# Create Bitmap
Bitmap cache = view.getDrawingCache();
# Save Bitmap
saveBitmap(cache);
view.destroyDrawingCache();
Compound Views
• Compound views are a very powerful way of reusing code and UI when
programming for Android. A Compound View is essentially a collection of
two or more other UI elements placed together, which you can use as if it
was a single View. For instance, you could use Compound Views to create
a ListView item which contains a small picture and some text. You can also
add custom logic to this Compound View.
Creating custom views
• By extending the View class or one of its subclasses you can create your custom
view.
• For drawing view use the onDraw() method. In this method you receive a Canvas
object which allows you to perform drawing operations on it, e.g. draw lines, circle,
text or bitmaps. If the view should be re-drawn you call the invalidate() method
which triggers a call to the onDraw() method of this view.
Defining custom layout managers
• You can implement your custom layout manager by extending the ViewGroup
class. This allows you to implement more efficient layout managers or to
implement effects which are currently missing in the Android platform.
• A custom layout manager can override the onMeasure() and onLayout() method
and specialize the calculation of its children. For example it can leave out the time
consuming support of layout_weight of the LinearLayout class.
Life Cycle
Life cycle events related to the window
• A view is displayed if it is attached to a layout hierarchy which is attached to a
window. A view has several life cycle hooks.
• The onAttachedToWindow() is called once the window is available.
• The onDetachedFromWindow() is used when the view is removed from its parent
(and if the parent is attached to a window). This happens for example if the activity
is recycled (e.g. via the finished() method call) or if the view is recycled in a
ListView. The onDetachedFromWindow() method can be used to stop animations
and to clean up resources used by the view.
Traversal life cycle events
• Traversals life cycle events consists of Animate, Measure, Layout and Draw.
• All views must know how to measure and layout themselves. The
requestLayout() method call tells the view to measure and layout itself. As
this operation may influence the layout of other views it calls also
requestLayout() of its parent.
• The onMeasure() method determines the size for the view and its children.
It must set the dimension via the setMeasuredDimension() method is this
method call before returning.
• The onLayout() positions the views based on the result of the onMeasure()
method call. This call happens typically once, whichonMeasure() can
happens once.
Creating custom views
Creation
Constructor:
There is a form of the constructor that are called when the view is created from code
and a form that is called when the view is inflated from a layout file. The second form
should parse and apply any attributes defined in the layout file.
OnFinishInflate()
Called after a view and all of its children has been inflated from XML.
This is called as the last phase of inflation, after all child views have been added
Creating custom views
Layout
onMeasure(int ,int)
Called to determine the size requirements for this view and all of its children.
When overriding this method, you must call setMeasuredDimension(int, int) measured
width and height of this view. Failure to do so will trigger an IllegalStateException
onLayout (boolean changed, int left, int top, int right, int bottom)
Called from layout when this view should assign a size and position to each of its
children. Derived classes with children should override this method and call layout on
each of their children.
protected void onSizeChanged (int w, int h, int oldw, int oldh)
This is called during layout when the size of this view has changed. If you were just
added to the view hierarchy, you're called with the old values of 0.
Creating custom views
Draw:
protected void onDraw (Canvas canvas)
Implement this to do your drawing.
Creating custom views
Event Processing:
onKeyDown(int, KeyEvent)
Called when a new hardware key event occurs.
onKeyUp(int, KeyEvent)
Called when a hardware key up event occurs.
onTrackballEvent(MotionEvent)
Called when a trackball motion event occurs.
onTouchEvent(MotionEvent)
Called when a touch screen motion event occurs.
Creating custom views
Focus:
onFocusChanged(boolean, int, android.graphics.Rect)
Called when the view gains or loses focus.
onWindowFocusChanged(boolean)
Called when the window containing the view gains or loses focus.
Creating custom views
Attaching:
onAttachedToWindow()
Called when the view is attached to a window.
onDetachedFromWindow()
Called when the view is detached from its window.
onWindowVisibilityChanged(int)
Called when the visibility of the window containing the view has changed.

More Related Content

What's hot (20)

Design concepts and principles
Design concepts and principlesDesign concepts and principles
Design concepts and principles
 
Android-dialogs in android-chapter14
Android-dialogs in android-chapter14Android-dialogs in android-chapter14
Android-dialogs in android-chapter14
 
Android graphics
Android graphicsAndroid graphics
Android graphics
 
Map reduce presentation
Map reduce presentationMap reduce presentation
Map reduce presentation
 
Javascript event handler
Javascript event handlerJavascript event handler
Javascript event handler
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
 
6.applet programming in java
6.applet programming in java6.applet programming in java
6.applet programming in java
 
Android intents
Android intentsAndroid intents
Android intents
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Data Storage In Android
Data Storage In Android Data Storage In Android
Data Storage In Android
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Android - Android Intent Types
Android - Android Intent TypesAndroid - Android Intent Types
Android - Android Intent Types
 
Android Fragment
Android FragmentAndroid Fragment
Android Fragment
 
Pressman ch-11-component-level-design
Pressman ch-11-component-level-designPressman ch-11-component-level-design
Pressman ch-11-component-level-design
 
Android datastorage
Android datastorageAndroid datastorage
Android datastorage
 
Ch 3 event driven programming
Ch 3 event driven programmingCh 3 event driven programming
Ch 3 event driven programming
 
Creating simple component
Creating simple componentCreating simple component
Creating simple component
 
Android installation
Android installationAndroid installation
Android installation
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 

Viewers also liked

Android design and Custom views
Android design and Custom views Android design and Custom views
Android design and Custom views Lars Vogel
 
Beyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and MoreBeyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and Morerogeryi
 
Android Development - ConstraintLayout
Android Development - ConstraintLayoutAndroid Development - ConstraintLayout
Android Development - ConstraintLayoutManuel Vicente Vivo
 
Journey of an event, the android touch - Marco Cova, Facebook
Journey of an event, the android touch - Marco Cova, FacebookJourney of an event, the android touch - Marco Cova, Facebook
Journey of an event, the android touch - Marco Cova, FacebookDroidConTLV
 
Acrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMAcrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMDong-Ho Lee
 
[Android] Widget Event Handling
[Android] Widget Event Handling[Android] Widget Event Handling
[Android] Widget Event HandlingNikmesoft Ltd
 
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspectiveAndroid graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspectiveBin Chen
 
Android internals 07 - Android graphics (rev_1.1)
Android internals 07 - Android graphics (rev_1.1)Android internals 07 - Android graphics (rev_1.1)
Android internals 07 - Android graphics (rev_1.1)Egor Elizarov
 
Android Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUKAndroid Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUKFabio Collini
 
Data Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternData Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternFabio Collini
 

Viewers also liked (13)

Android design and Custom views
Android design and Custom views Android design and Custom views
Android design and Custom views
 
Beyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and MoreBeyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and More
 
Android Development - ConstraintLayout
Android Development - ConstraintLayoutAndroid Development - ConstraintLayout
Android Development - ConstraintLayout
 
Journey of an event, the android touch - Marco Cova, Facebook
Journey of an event, the android touch - Marco Cova, FacebookJourney of an event, the android touch - Marco Cova, Facebook
Journey of an event, the android touch - Marco Cova, Facebook
 
Introduction to Android Window System
Introduction to Android Window SystemIntroduction to Android Window System
Introduction to Android Window System
 
Acrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVMAcrhitecture deisign pattern_MVC_MVP_MVVM
Acrhitecture deisign pattern_MVC_MVP_MVVM
 
АСУ ЖК
АСУ ЖКАСУ ЖК
АСУ ЖК
 
[Android] Widget Event Handling
[Android] Widget Event Handling[Android] Widget Event Handling
[Android] Widget Event Handling
 
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspectiveAndroid graphic system (SurfaceFlinger) : Design Pattern's perspective
Android graphic system (SurfaceFlinger) : Design Pattern's perspective
 
Android internals 07 - Android graphics (rev_1.1)
Android internals 07 - Android graphics (rev_1.1)Android internals 07 - Android graphics (rev_1.1)
Android internals 07 - Android graphics (rev_1.1)
 
Android Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUKAndroid Data Binding in action using MVVM pattern - droidconUK
Android Data Binding in action using MVVM pattern - droidconUK
 
Data Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternData Binding in Action using MVVM pattern
Data Binding in Action using MVVM pattern
 
Design and Concepts of Android Graphics
Design and Concepts of Android GraphicsDesign and Concepts of Android Graphics
Design and Concepts of Android Graphics
 

Similar to Android Custom Views

Ch4 creating user interfaces
Ch4 creating user interfacesCh4 creating user interfaces
Ch4 creating user interfacesShih-Hsiang Lin
 
Android App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsAndroid App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsDiego Grancini
 
Advance ui development and design
Advance ui  development and design Advance ui  development and design
Advance ui development and design Rakesh Jha
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptxMugiiiReee
 
User experience and interactions design
User experience and interactions design User experience and interactions design
User experience and interactions design Rakesh Jha
 
Session 15 - Working with Image, Scroll, Collection, Picker, and Web View
Session 15  - Working with Image, Scroll, Collection, Picker, and Web ViewSession 15  - Working with Image, Scroll, Collection, Picker, and Web View
Session 15 - Working with Image, Scroll, Collection, Picker, and Web ViewVu Tran Lam
 
04 user interfaces
04 user interfaces04 user interfaces
04 user interfacesC.o. Nieto
 
Gitter marionette deck
Gitter marionette deckGitter marionette deck
Gitter marionette deckMike Bartlett
 
Android Custom views
Android Custom views   Android Custom views
Android Custom views Matej Vukosav
 
Android webinar class_4
Android webinar class_4Android webinar class_4
Android webinar class_4Edureka!
 
Introduction of Android View
Introduction of Android ViewIntroduction of Android View
Introduction of Android ViewCharile Tsai
 
W1_Lec01_Lec02_Layouts.pptx
W1_Lec01_Lec02_Layouts.pptxW1_Lec01_Lec02_Layouts.pptx
W1_Lec01_Lec02_Layouts.pptxssuserc1e786
 
Porting the Legacy Application to Composite Application Guidance
Porting the Legacy Application to Composite Application GuidancePorting the Legacy Application to Composite Application Guidance
Porting the Legacy Application to Composite Application GuidanceOur Community Exchange LLC
 
iOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkiOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkMiguel de Icaza
 
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficientTh 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficientBin Shao
 
Synapse india reviews on i phone and android os
Synapse india reviews on i phone and android osSynapse india reviews on i phone and android os
Synapse india reviews on i phone and android ossaritasingh19866
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsSubhransu Behera
 

Similar to Android Custom Views (20)

Custom components
Custom componentsCustom components
Custom components
 
Ch4 creating user interfaces
Ch4 creating user interfacesCh4 creating user interfaces
Ch4 creating user interfaces
 
Android App Development - 04 Views and layouts
Android App Development - 04 Views and layoutsAndroid App Development - 04 Views and layouts
Android App Development - 04 Views and layouts
 
Advance ui development and design
Advance ui  development and design Advance ui  development and design
Advance ui development and design
 
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx11.11.2020 - Unit 5-3  ACTIVITY, MENU AND SQLITE DATABASE.pptx
11.11.2020 - Unit 5-3 ACTIVITY, MENU AND SQLITE DATABASE.pptx
 
User experience and interactions design
User experience and interactions design User experience and interactions design
User experience and interactions design
 
Session 15 - Working with Image, Scroll, Collection, Picker, and Web View
Session 15  - Working with Image, Scroll, Collection, Picker, and Web ViewSession 15  - Working with Image, Scroll, Collection, Picker, and Web View
Session 15 - Working with Image, Scroll, Collection, Picker, and Web View
 
04 user interfaces
04 user interfaces04 user interfaces
04 user interfaces
 
Gitter marionette deck
Gitter marionette deckGitter marionette deck
Gitter marionette deck
 
Android Custom views
Android Custom views   Android Custom views
Android Custom views
 
Diving Into Xamarin.Forms
Diving Into Xamarin.Forms Diving Into Xamarin.Forms
Diving Into Xamarin.Forms
 
Android webinar class_4
Android webinar class_4Android webinar class_4
Android webinar class_4
 
Introduction of Android View
Introduction of Android ViewIntroduction of Android View
Introduction of Android View
 
Hello Android
Hello AndroidHello Android
Hello Android
 
W1_Lec01_Lec02_Layouts.pptx
W1_Lec01_Lec02_Layouts.pptxW1_Lec01_Lec02_Layouts.pptx
W1_Lec01_Lec02_Layouts.pptx
 
Porting the Legacy Application to Composite Application Guidance
Porting the Legacy Application to Composite Application GuidancePorting the Legacy Application to Composite Application Guidance
Porting the Legacy Application to Composite Application Guidance
 
iOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections TalkiOS for C# Developers - DevConnections Talk
iOS for C# Developers - DevConnections Talk
 
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficientTh 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
 
Synapse india reviews on i phone and android os
Synapse india reviews on i phone and android osSynapse india reviews on i phone and android os
Synapse india reviews on i phone and android os
 
iOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIsiOS 101 - Xcode, Objective-C, iOS APIs
iOS 101 - Xcode, Objective-C, iOS APIs
 

Recently uploaded

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 

Android Custom Views

  • 2. • Name: Babar Sana • Department: Android • Designation: Software Engineer
  • 3. Table of Contents 1.Custom Views • Default views • How Android Draws views • Reason for creating views • Responsibility of views • Using new views in your layout files • Create Screenshot of Views
  • 4. Table of Contents 2.Compound Views 3.Creating Custom Views • Creating Custom Views • Measurements • Defining Custom layout manager 4.Life Cycle •Life Cycle Events related to Windows •Traversal life cycle Events •Activity Life Cycle
  • 6. 1.Custom Views The Android framework provides several default views but developer can also create their custom views and use them in their application. The base class a view is the View.
  • 7. How Android Draws Views • Once an Activity receives the focus, it must provide the root node of its layout hierarchy to the Android system. Afterwards the Android system starts the drawing procedure. • Drawing begins with the root node of the layout. The layout hierarchy is traversed in the order of declaration, i.e., parents are drawn before their children and children are drawn in the order of declaration. • Drawing the layout is a two pass process: • measuring pass - implemented in the measure(int, int) method and is a top-down traversal of the view hierarchy. Every view stores its measurements. • layout pass - implemented in the layout(int, int, int, int) method is also a top-down traversal of the view hierarchy. During this phase each layout manager is responsible for positioning all of its children. It uses the the sizes computed in the measure pass.
  • 8. How Android Draws Views • A view or activity can retrigger the measure and layout pass with a call to the requestLayout() method. • After the measure and layout calculation, the views draw themselves. This operation can be triggered with the invalidate() method from the View class.
  • 9. Reasons for Creating Views • View are typically created to provide a user interface experience with is not possible with the default views. • Using custom view allows the developer allow to do certain performance optimization
  • 10. Responsibility of Views • Views are responsible for measuring, layouting and drawing themselves and their child elements (in case of a ViewGroup) • Views are also responsible for saving their UI state and handling touch events
  • 11. Ways of creating custom views • Customs views are typically classified as compound views or custom views. It is possible to create custom views by: • Compound views • Custom Views – By extending an existing view • By extending the View class
  • 12. Using new views in layout files • Custom and compound views can be used in layout files. For this you need to use the full qualified name in the layout file, e.g. using the package and class name. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <de.android.ownview.MyDrawView android:id="@+id/myDrawView1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
  • 13. Create screenshots (Images) of views • Every View class support the creating of an image of its current display. The following coding shows an example for that. # Build the Drawing Cache view.buildDrawingCache(); # Create Bitmap Bitmap cache = view.getDrawingCache(); # Save Bitmap saveBitmap(cache); view.destroyDrawingCache();
  • 14. Compound Views • Compound views are a very powerful way of reusing code and UI when programming for Android. A Compound View is essentially a collection of two or more other UI elements placed together, which you can use as if it was a single View. For instance, you could use Compound Views to create a ListView item which contains a small picture and some text. You can also add custom logic to this Compound View.
  • 15. Creating custom views • By extending the View class or one of its subclasses you can create your custom view. • For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps. If the view should be re-drawn you call the invalidate() method which triggers a call to the onDraw() method of this view. Defining custom layout managers • You can implement your custom layout manager by extending the ViewGroup class. This allows you to implement more efficient layout managers or to implement effects which are currently missing in the Android platform. • A custom layout manager can override the onMeasure() and onLayout() method and specialize the calculation of its children. For example it can leave out the time consuming support of layout_weight of the LinearLayout class.
  • 16. Life Cycle Life cycle events related to the window • A view is displayed if it is attached to a layout hierarchy which is attached to a window. A view has several life cycle hooks. • The onAttachedToWindow() is called once the window is available. • The onDetachedFromWindow() is used when the view is removed from its parent (and if the parent is attached to a window). This happens for example if the activity is recycled (e.g. via the finished() method call) or if the view is recycled in a ListView. The onDetachedFromWindow() method can be used to stop animations and to clean up resources used by the view.
  • 17. Traversal life cycle events • Traversals life cycle events consists of Animate, Measure, Layout and Draw. • All views must know how to measure and layout themselves. The requestLayout() method call tells the view to measure and layout itself. As this operation may influence the layout of other views it calls also requestLayout() of its parent. • The onMeasure() method determines the size for the view and its children. It must set the dimension via the setMeasuredDimension() method is this method call before returning. • The onLayout() positions the views based on the result of the onMeasure() method call. This call happens typically once, whichonMeasure() can happens once.
  • 18. Creating custom views Creation Constructor: There is a form of the constructor that are called when the view is created from code and a form that is called when the view is inflated from a layout file. The second form should parse and apply any attributes defined in the layout file. OnFinishInflate() Called after a view and all of its children has been inflated from XML. This is called as the last phase of inflation, after all child views have been added
  • 19. Creating custom views Layout onMeasure(int ,int) Called to determine the size requirements for this view and all of its children. When overriding this method, you must call setMeasuredDimension(int, int) measured width and height of this view. Failure to do so will trigger an IllegalStateException onLayout (boolean changed, int left, int top, int right, int bottom) Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children. protected void onSizeChanged (int w, int h, int oldw, int oldh) This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.
  • 20. Creating custom views Draw: protected void onDraw (Canvas canvas) Implement this to do your drawing.
  • 21. Creating custom views Event Processing: onKeyDown(int, KeyEvent) Called when a new hardware key event occurs. onKeyUp(int, KeyEvent) Called when a hardware key up event occurs. onTrackballEvent(MotionEvent) Called when a trackball motion event occurs. onTouchEvent(MotionEvent) Called when a touch screen motion event occurs.
  • 22. Creating custom views Focus: onFocusChanged(boolean, int, android.graphics.Rect) Called when the view gains or loses focus. onWindowFocusChanged(boolean) Called when the window containing the view gains or loses focus.
  • 23. Creating custom views Attaching: onAttachedToWindow() Called when the view is attached to a window. onDetachedFromWindow() Called when the view is detached from its window. onWindowVisibilityChanged(int) Called when the visibility of the window containing the view has changed.