SlideShare a Scribd company logo
1 of 60
Building a Consistent User
Experience Across a Range of
Android Devices
Irene Duke
Senior Android Engineer
Prolific Interactive
Feb. 3, 2015
Agenda
• Why is consistent UI important?
• Learn about xml resources such as dimens, drawables,
colors, styles, and themes
• Learn to use the Android support libraries for backwards
compatibility
• Learn to create layouts that adapt to different screen sizes
• Learn to leverage open source solutions when the Android
support libraries fall short
• Learn to create re-usable UI components and custom views
Why is this important?
SDK Fragmentation
Source: Google
Screen Size and Density
Fragmentation
Source: Google
Device Manufacturer
Fragmentation
XML Resources and
Qualifiers
XML Resources
• layouts
• themes, styles
• dimens
• drawables
• colors
• anim, animator, strings, selectors, and more
Resource Qualifiers
• Qualifiers allow us to provide alternate resources for
different devices and/or configurations
• Create a new directory in res/ named in the form
<resources_name>-<qualifier>
• <resources_name> is the directory name of the
corresponding default resources
• <qualifier> is a name that specifies an individual
configuration for which these resources are to be
used
Resource Qualifiers
• Can be chained with a dash
between qualifiers
• Android system finds the “Best
Matching” resource at runtime
based on the qualifiers
Resource Qualifiers
• screen resolution (dpi): -mdpi, -ldpi, -hdpi, -xhdpi, -
xxhdpi, -xxxhdpi (launcher icons only), -tvdpi
• orientation: -land
• screen size: -small, -large, -sw600dp, -w720dp, -
h720dp
• SDK version: -v21
Colors
colors.xml in res/values
Colors
• Use these in other xml resources by referring to
them as
@color/color_primary
• Use them in code using
getResources().getColor(R.color.color_primary)
DRY (Don’t Repeat
Yourself)
• Define all colors you want to use in colors.xml
• Reference the definition
• Use code completion
• Less possibility for errors
dimens.xml
• density independent pixels (dp or dip)
• scale independent pixels (sp)
• never use absolute pixels (px)
Density Independent Pixels
• The Android system scales these at runtime based
on the device screen density (pixels per inch)
• On an mdpi (~160dpi) screen, 8dp = 8px
• ldpi : mdpi : hdpi : xhdpi : xxhdpi
3 : 4 : 6 : 8 : 12
Scale Independent Pixels
• Used for text
• The Android system scales these for the current
screen density and the user’s system-wide scaling
factor for text selected in the Settings app
dimens.xml
in the res/values folder
dimens.xml
in the res/values-w820dp folder
Dimens
• Use these in other xml resources by referring to
them as
@dimen/text_size_small
• Use them in code using
getResources()
.getDimensionPixelSize(R.dimen.padding_small)
XML Drawables
in the res/drawables folder
PNG Drawables
• Android groups all actual screen densities into six
generalized densities: ldpi, mdpi, hdpi, xhdpi,
xxhdpi, and xxxhdpi
• Provide .png assets for each density you want to
target to prevent Android scaling images at runtime
• If apk size is a concern, provide xxhdpi and mdpi
only and let the Android system scale drawables for
other devices
Styles
• A collection of properties that customise the way a
view should be drawn
• Set properties such as padding, font size,
background color, and much more
styles.xml
in the res/values folder
Applying Styles
Apply the style in layouts
or in themes.xml
DRY (Don’t Repeat
Yourself)
• Define styles for common components
• Reference the definition
• Use code completion
• Less possibility for errors
Themes
• Styles that are applied on an application or activity
level, usually in the AndroidManifest
• You shouldn’t have a ton of themes. Most activities
should use a consistent theme.
• For backwards compatibility, extend from
AppCompat themes in the support libraries
Material (AppCompat)
Theme
themes.xml
in the res/values folder
themes.xml
in AndroidManifest
Backwards
Compatibility
Evolution of Android
• Pre-Android 4.0 - Base themes are fully customisable by
the device manufacturer
• Android 4.0 - Holo Light and Holo Dark themes are
available on all devices that have the Google Play store.
AppCompat theme available in support library to bring
Holo theme to pre-Android 4.0 devices
• Android 5.0 - Material Light and Material Dark themes are
available on all devices that have the Google Play store.
AppCompat theme in support library is updated to
Material design
Support libraries
• Released by Google to provide backwards
compatibility for some themes, styles, and SDK
features
• Use them whenever possible to make your app look
the same across SDK versions and manufacturers
Using the support libraries
in build.gradle
Use AppCompat
• All Activities should extend from ActionBarActivity
• All themes must inherit from Theme.AppCompat
• use ActionBar, Toolbar, DrawerLayout,
NotificationCompat, MenuItemCompat, Fragment
and other APIs from the support library (make sure
the import is correct)
Only in Lollipop
• Elevation (z-ordering and shadows)
• Ripple effect
• New activity transitions like explode and shared
element transitions
Graceful Fallbacks
add tools:ignore=“NewApi" in xml
or
Layouts
Preview Pane
• Allows you to see how your layout will adapt to
different configurations
• It’s your friend (but sometimes it has bugs)
Design Time Layout Attributes
(Tools Namespace)
• No runtime overhead
• All attributes you define in the tools namespace are
stripped during the build process
Using the Tools Namespace
Layouts that Adapt
• FrameLayout
• LinearLayout
• RelativeLayout
• Never use AbsoluteLayout
Width and Height
• Must be specified for all views and layouts
• Use dp, wrap_content, match_parent, and gravity
• Never use px
Gravity
• gravity - sets the gravity of the content within the
view (or layout) its used on
• layout_gravity - sets the gravity of the view (or
layout) within its parent
• This can be tricky (remember the preview pane is
your friend)
FrameLayout
• Views are layered in a frame
• Views can be centered or aligned with the edges of
the frame using layout_gravity
• Let’s take a look at some code
LinearLayout
• Views are drawn one after another, either
horizontally or vertically
• Weights can be used to change how much space a
view gets
• Let’s take a look at some code
LinearLayout
• Using weight with LinearLayout causes views to be
measured twice during layout.
• When a LinearLayout with non-zero weights is nested
inside another LinearLayout with non-zero weights, then
the number of measurements increase exponentially.
• Use 0dp instead of wrap_content or match_parent in
the direction this view should grow
• Flatten the hierarchy with RelativeLayout
RelativeLayout
• Most flexible layout
• Views can be positioned in relation to other views
• Let’s take a look at some code
3rd Party Libraries
Material Design
• Android L is missing implementations for some
components like floating action buttons, snackbars,
and more.
• Support libraries are missing backwards
compatibility for styling of some components like
alert dialogs, date and time pickers, progress
spinners, and more.
Open Source
• Don’t re-invent the wheel
• Many Android developers have released open source
solutions for the missing or imperfect components
• Floating Action Button
• Snackbar
• Alert Dialog
• Edit Text
What to Look for in an Open
Source Library
• Lots of stars
• Recent activity/releases
• Documentation, customisability
• Open source license like MIT or Apache
• Availability on maven central or other central repository
• Doesn’t throw warnings or cause crashes
Gradle
A build system that makes it easy to leverage open
source solutions
Calligraphy
• A library to help with fonts
• https://github.com/chrisjenx/Calligraphy
Material Dialog
• AlertDialog is styled differently on different platform
versions
• https://github.com/afollestad/material-dialogs
Material EditText
• EditText is styled differently on different platform
versions
• https://github.com/afollestad/material-dialogs
Floating Action Button
• Floating action buttons aren’t built in to Android
• https://github.com/makovkastar/FloatingActionButto
n
Snackbar
• Snackbars aren’t built in to Android
• https://github.com/nispok/snackbar
Re-Usable UI and
Custom Views
Custom Views
• Let’s take a look at some code

More Related Content

Similar to Consistent UI Across Android Devices

Android application development
Android application developmentAndroid application development
Android application developmentLinh Vi Tường
 
Atlanta Drupal User Group (ADUG)
Atlanta Drupal User Group (ADUG) Atlanta Drupal User Group (ADUG)
Atlanta Drupal User Group (ADUG) Mediacurrent
 
Android webinar class_1
Android webinar class_1Android webinar class_1
Android webinar class_1Edureka!
 
Android Studio development model and.pptx
Android Studio development model and.pptxAndroid Studio development model and.pptx
Android Studio development model and.pptxVaibhavKhunger2
 
Android development orientation for starters v2
Android development orientation for starters v2Android development orientation for starters v2
Android development orientation for starters v2Joemarie Amparo
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android StudioSuyash Srijan
 
Anatomy Of An Adroid Application Lecture 3.pptx
Anatomy Of An Adroid Application Lecture 3.pptxAnatomy Of An Adroid Application Lecture 3.pptx
Anatomy Of An Adroid Application Lecture 3.pptxMuzamil Yousaf
 
Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013
Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013
Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013DuckMa
 
Browser Developer Tools for APEX Developers
Browser Developer Tools for APEX DevelopersBrowser Developer Tools for APEX Developers
Browser Developer Tools for APEX DevelopersChristian Rokitta
 
Introduction to Android (before 2015)
Introduction to Android (before 2015)Introduction to Android (before 2015)
Introduction to Android (before 2015)Chien-Ming Chou
 
Infinite Scale - Introduction to Google App Engine
Infinite Scale - Introduction to Google App EngineInfinite Scale - Introduction to Google App Engine
Infinite Scale - Introduction to Google App EngineMarian Borca
 
Android application development fundamentals
Android application development fundamentalsAndroid application development fundamentals
Android application development fundamentalsindiangarg
 
5 Keys to API Design - API Days Paris 2013
5 Keys to API Design - API Days Paris 20135 Keys to API Design - API Days Paris 2013
5 Keys to API Design - API Days Paris 2013Daniel Feist
 
Code the docs-yu liu
Code the docs-yu liuCode the docs-yu liu
Code the docs-yu liuStreamNative
 
SEF 2014 - Responsive Design in SharePoint 2013
SEF 2014 - Responsive Design in SharePoint 2013SEF 2014 - Responsive Design in SharePoint 2013
SEF 2014 - Responsive Design in SharePoint 2013Marc D Anderson
 

Similar to Consistent UI Across Android Devices (20)

Android application development
Android application developmentAndroid application development
Android application development
 
Atlanta Drupal User Group (ADUG)
Atlanta Drupal User Group (ADUG) Atlanta Drupal User Group (ADUG)
Atlanta Drupal User Group (ADUG)
 
Android webinar class_1
Android webinar class_1Android webinar class_1
Android webinar class_1
 
Android Studio development model and.pptx
Android Studio development model and.pptxAndroid Studio development model and.pptx
Android Studio development model and.pptx
 
Android development orientation for starters v2
Android development orientation for starters v2Android development orientation for starters v2
Android development orientation for starters v2
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android Studio
 
Android
Android Android
Android
 
Anatomy Of An Adroid Application Lecture 3.pptx
Anatomy Of An Adroid Application Lecture 3.pptxAnatomy Of An Adroid Application Lecture 3.pptx
Anatomy Of An Adroid Application Lecture 3.pptx
 
Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013
Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013
Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013
 
Android bootcamp-day1
Android bootcamp-day1Android bootcamp-day1
Android bootcamp-day1
 
Intro to android (gdays)
Intro to android (gdays)Intro to android (gdays)
Intro to android (gdays)
 
Browser Developer Tools for APEX Developers
Browser Developer Tools for APEX DevelopersBrowser Developer Tools for APEX Developers
Browser Developer Tools for APEX Developers
 
Introduction to Android (before 2015)
Introduction to Android (before 2015)Introduction to Android (before 2015)
Introduction to Android (before 2015)
 
Infinite Scale - Introduction to Google App Engine
Infinite Scale - Introduction to Google App EngineInfinite Scale - Introduction to Google App Engine
Infinite Scale - Introduction to Google App Engine
 
Android application development fundamentals
Android application development fundamentalsAndroid application development fundamentals
Android application development fundamentals
 
The Developers World
The Developers WorldThe Developers World
The Developers World
 
5 Keys to API Design - API Days Paris 2013
5 Keys to API Design - API Days Paris 20135 Keys to API Design - API Days Paris 2013
5 Keys to API Design - API Days Paris 2013
 
Code the docs-yu liu
Code the docs-yu liuCode the docs-yu liu
Code the docs-yu liu
 
UI and UX for Mobile Developers
UI and UX for Mobile DevelopersUI and UX for Mobile Developers
UI and UX for Mobile Developers
 
SEF 2014 - Responsive Design in SharePoint 2013
SEF 2014 - Responsive Design in SharePoint 2013SEF 2014 - Responsive Design in SharePoint 2013
SEF 2014 - Responsive Design in SharePoint 2013
 

Recently uploaded

GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Developmentvyaparkranti
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 

Recently uploaded (20)

GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
VK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web DevelopmentVK Business Profile - provides IT solutions and Web Development
VK Business Profile - provides IT solutions and Web Development
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 

Consistent UI Across Android Devices

  • 1. Building a Consistent User Experience Across a Range of Android Devices Irene Duke Senior Android Engineer Prolific Interactive Feb. 3, 2015
  • 2. Agenda • Why is consistent UI important? • Learn about xml resources such as dimens, drawables, colors, styles, and themes • Learn to use the Android support libraries for backwards compatibility • Learn to create layouts that adapt to different screen sizes • Learn to leverage open source solutions when the Android support libraries fall short • Learn to create re-usable UI components and custom views
  • 3. Why is this important?
  • 5. Screen Size and Density Fragmentation Source: Google
  • 8. XML Resources • layouts • themes, styles • dimens • drawables • colors • anim, animator, strings, selectors, and more
  • 9. Resource Qualifiers • Qualifiers allow us to provide alternate resources for different devices and/or configurations • Create a new directory in res/ named in the form <resources_name>-<qualifier> • <resources_name> is the directory name of the corresponding default resources • <qualifier> is a name that specifies an individual configuration for which these resources are to be used
  • 10. Resource Qualifiers • Can be chained with a dash between qualifiers • Android system finds the “Best Matching” resource at runtime based on the qualifiers
  • 11. Resource Qualifiers • screen resolution (dpi): -mdpi, -ldpi, -hdpi, -xhdpi, - xxhdpi, -xxxhdpi (launcher icons only), -tvdpi • orientation: -land • screen size: -small, -large, -sw600dp, -w720dp, - h720dp • SDK version: -v21
  • 13. Colors • Use these in other xml resources by referring to them as @color/color_primary • Use them in code using getResources().getColor(R.color.color_primary)
  • 14. DRY (Don’t Repeat Yourself) • Define all colors you want to use in colors.xml • Reference the definition • Use code completion • Less possibility for errors
  • 15. dimens.xml • density independent pixels (dp or dip) • scale independent pixels (sp) • never use absolute pixels (px)
  • 16. Density Independent Pixels • The Android system scales these at runtime based on the device screen density (pixels per inch) • On an mdpi (~160dpi) screen, 8dp = 8px • ldpi : mdpi : hdpi : xhdpi : xxhdpi 3 : 4 : 6 : 8 : 12
  • 17. Scale Independent Pixels • Used for text • The Android system scales these for the current screen density and the user’s system-wide scaling factor for text selected in the Settings app
  • 20. Dimens • Use these in other xml resources by referring to them as @dimen/text_size_small • Use them in code using getResources() .getDimensionPixelSize(R.dimen.padding_small)
  • 21. XML Drawables in the res/drawables folder
  • 22. PNG Drawables • Android groups all actual screen densities into six generalized densities: ldpi, mdpi, hdpi, xhdpi, xxhdpi, and xxxhdpi • Provide .png assets for each density you want to target to prevent Android scaling images at runtime • If apk size is a concern, provide xxhdpi and mdpi only and let the Android system scale drawables for other devices
  • 23. Styles • A collection of properties that customise the way a view should be drawn • Set properties such as padding, font size, background color, and much more
  • 25. Applying Styles Apply the style in layouts or in themes.xml
  • 26. DRY (Don’t Repeat Yourself) • Define styles for common components • Reference the definition • Use code completion • Less possibility for errors
  • 27. Themes • Styles that are applied on an application or activity level, usually in the AndroidManifest • You shouldn’t have a ton of themes. Most activities should use a consistent theme. • For backwards compatibility, extend from AppCompat themes in the support libraries
  • 32. Evolution of Android • Pre-Android 4.0 - Base themes are fully customisable by the device manufacturer • Android 4.0 - Holo Light and Holo Dark themes are available on all devices that have the Google Play store. AppCompat theme available in support library to bring Holo theme to pre-Android 4.0 devices • Android 5.0 - Material Light and Material Dark themes are available on all devices that have the Google Play store. AppCompat theme in support library is updated to Material design
  • 33. Support libraries • Released by Google to provide backwards compatibility for some themes, styles, and SDK features • Use them whenever possible to make your app look the same across SDK versions and manufacturers
  • 34. Using the support libraries in build.gradle
  • 35. Use AppCompat • All Activities should extend from ActionBarActivity • All themes must inherit from Theme.AppCompat • use ActionBar, Toolbar, DrawerLayout, NotificationCompat, MenuItemCompat, Fragment and other APIs from the support library (make sure the import is correct)
  • 36. Only in Lollipop • Elevation (z-ordering and shadows) • Ripple effect • New activity transitions like explode and shared element transitions
  • 39. Preview Pane • Allows you to see how your layout will adapt to different configurations • It’s your friend (but sometimes it has bugs)
  • 40. Design Time Layout Attributes (Tools Namespace) • No runtime overhead • All attributes you define in the tools namespace are stripped during the build process
  • 41. Using the Tools Namespace
  • 42. Layouts that Adapt • FrameLayout • LinearLayout • RelativeLayout • Never use AbsoluteLayout
  • 43. Width and Height • Must be specified for all views and layouts • Use dp, wrap_content, match_parent, and gravity • Never use px
  • 44. Gravity • gravity - sets the gravity of the content within the view (or layout) its used on • layout_gravity - sets the gravity of the view (or layout) within its parent • This can be tricky (remember the preview pane is your friend)
  • 45. FrameLayout • Views are layered in a frame • Views can be centered or aligned with the edges of the frame using layout_gravity • Let’s take a look at some code
  • 46. LinearLayout • Views are drawn one after another, either horizontally or vertically • Weights can be used to change how much space a view gets • Let’s take a look at some code
  • 47. LinearLayout • Using weight with LinearLayout causes views to be measured twice during layout. • When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially. • Use 0dp instead of wrap_content or match_parent in the direction this view should grow • Flatten the hierarchy with RelativeLayout
  • 48. RelativeLayout • Most flexible layout • Views can be positioned in relation to other views • Let’s take a look at some code
  • 50. Material Design • Android L is missing implementations for some components like floating action buttons, snackbars, and more. • Support libraries are missing backwards compatibility for styling of some components like alert dialogs, date and time pickers, progress spinners, and more.
  • 51. Open Source • Don’t re-invent the wheel • Many Android developers have released open source solutions for the missing or imperfect components • Floating Action Button • Snackbar • Alert Dialog • Edit Text
  • 52. What to Look for in an Open Source Library • Lots of stars • Recent activity/releases • Documentation, customisability • Open source license like MIT or Apache • Availability on maven central or other central repository • Doesn’t throw warnings or cause crashes
  • 53. Gradle A build system that makes it easy to leverage open source solutions
  • 54. Calligraphy • A library to help with fonts • https://github.com/chrisjenx/Calligraphy
  • 55. Material Dialog • AlertDialog is styled differently on different platform versions • https://github.com/afollestad/material-dialogs
  • 56. Material EditText • EditText is styled differently on different platform versions • https://github.com/afollestad/material-dialogs
  • 57. Floating Action Button • Floating action buttons aren’t built in to Android • https://github.com/makovkastar/FloatingActionButto n
  • 58. Snackbar • Snackbars aren’t built in to Android • https://github.com/nispok/snackbar
  • 60. Custom Views • Let’s take a look at some code