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

Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and EventsHenry Osborne
 
Android animation
Android animationAndroid animation
Android animationKrazy Koder
 
Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room LibraryReinvently
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & viewsma-polimi
 
React for Re-use: Creating UI Components with Confluence Connect
React for Re-use: Creating UI Components with Confluence ConnectReact for Re-use: Creating UI Components with Confluence Connect
React for Re-use: Creating UI Components with Confluence ConnectAtlassian
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming BasicsEueung Mulyana
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Androidma-polimi
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android FragmentsSergi Martínez
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to androidzeelpatel0504
 
Mobile Application Development With Android
Mobile Application Development With AndroidMobile Application Development With Android
Mobile Application Development With Androidguest213e237
 
Project meeting: Android Graphics Architecture Overview
Project meeting: Android Graphics Architecture OverviewProject meeting: Android Graphics Architecture Overview
Project meeting: Android Graphics Architecture OverviewYu-Hsin Hung
 
Broadcast Receivers in Android
Broadcast Receivers in AndroidBroadcast Receivers in Android
Broadcast Receivers in Androidma-polimi
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in androidPrawesh Shrestha
 
Unreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior TreesUnreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior TreesNick Pruehs
 

What's hot (20)

Android Intent.pptx
Android Intent.pptxAndroid Intent.pptx
Android Intent.pptx
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
 
Android animation
Android animationAndroid animation
Android animation
 
Data Persistence in Android with Room Library
Data Persistence in Android with Room LibraryData Persistence in Android with Room Library
Data Persistence in Android with Room Library
 
Android intents
Android intentsAndroid intents
Android intents
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Android Services
Android ServicesAndroid Services
Android Services
 
Android Location and Maps
Android Location and MapsAndroid Location and Maps
Android Location and Maps
 
React for Re-use: Creating UI Components with Confluence Connect
React for Re-use: Creating UI Components with Confluence ConnectReact for Re-use: Creating UI Components with Confluence Connect
React for Re-use: Creating UI Components with Confluence Connect
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming Basics
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
 
Introduction to Android Fragments
Introduction to Android FragmentsIntroduction to Android Fragments
Introduction to Android Fragments
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to android
 
Mobile Application Development With Android
Mobile Application Development With AndroidMobile Application Development With Android
Mobile Application Development With Android
 
Project meeting: Android Graphics Architecture Overview
Project meeting: Android Graphics Architecture OverviewProject meeting: Android Graphics Architecture Overview
Project meeting: Android Graphics Architecture Overview
 
Broadcast Receivers in Android
Broadcast Receivers in AndroidBroadcast Receivers in Android
Broadcast Receivers in Android
 
05 intent
05 intent05 intent
05 intent
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Unreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior TreesUnreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior Trees
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
 

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
 
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 (12)

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
 
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

costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 

Recently uploaded (20)

costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 

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.