SlideShare a Scribd company logo
1 of 40
Download to read offline
Resources System
What is Resources?
It takes more than just code to
build a great app. Resources are
the addi8onal files and sta8c
content that your code uses, such
as bitmaps, layout defini8ons,
user interface strings, anima8on
instruc8ons, and more.
What is Resources?
A Storeroom
contains pieces of data needed in
applica8on
•  Image (jpg, png)
•  Layout (xml)
•  etc.
What is Resources?
Java Source Codes
Resources
What is Resources?
Source Code Resources Running Applica5on
=+ +
+
What is Resources?
How does Resources System
look like?
You should always externalize resources
such as images and strings from your
applica8on code, so that you can maintain
them independently
Resources Sources
App-Specific Resources System Resources
What is Resources?
+
It’s awesomeIt works
Resources Type
Drawable
Something that can be
drawn on screen
Layout
XML contains Layout hierarchy/
structure
Menu
Context menu, Popup menu
Values
Strings, Colors, Dimension, etc.
Anima8on
XML contains Property
anima8on or View anima8on
etc.
Almost everything but Java
source codes
Resources Type: Drawable
•  A drawable resource is a general
concept for a graphic that can be
drawn to the screen
•  folder res/drawable
•  Have A LOT to be learned, be
prepared …
Playaround: ImageView
… Play Play Play …
How to refer resource from another resource?
… Play Play Play …
Rule 11:
Each resource folder couldn’t have
any child folder. Manage filename
wisely.
The R class: How are Resources compiled?
Filename will be simply
automa8cally turned into a int
variable name under
<package_name>.R.<resource_type>
We call it “Resource ID”
R.drawable.ic_launcher
Ic_launcher.png
Remember !
Rule 12:
Filename (ext not included) in
resource folder will be turned into a
variable name in Java. So you
can’t use some character in the
filename (for example, - +)
Rule 13:
Never has the same filename in the
same folder although they are in the
different file type (for example:
icon1.jpg, icon1.png)
Resources Type: Layout
•  A layout resource defines the
architecture for the UI in an
Ac8vity or a component of a UI.
•  Folder res/layout
The R class: How are Resources compiled? #2
Every single resource will be put in
the same place with different
assigned Resource ID automa8cally
set by Android compiler
R.drawable.ic_launcher
ac8vity_main.xml
R.layout.ac8vity_main
android:id
•  Inside the layout xml file, you will find something like
android:id=“@+id/xxxx” or android:id=“@id/xxxx”
•  It is simply a Resource ID referring to a View inside a Layout
•  You have no need to assign it for every single element, just one you need to
reference
Rule 14:
Don’t use the same ID in the single
file, but feel free to use in any other
file
Resources Type: Menu
•  A menu resource defines an
applica8on menu (Op8ons
Menu, Context Menu, or
submenu) that can be inflated
with MenuInflater.
•  Folder res/menu
•  Learn more about Menus in Part
3
Resources Type: Values
•  Folder res/values
•  Be careful, Resource ID is not R.values.xxxx
•  Look into each one by one
•  Dimension
•  String
•  Colors
•  You will learn more by prac8ce all along this class
Resources Type: Anim
•  View Anima8on
•  Creates an anima8on by performing a series of transforma8ons on a single
image or creates an anima8on by showing a sequence of images in order
•  Folder res/anim
•  Too complicated to talk about this now. See you later.
Resources Type: Animator
•  Property Anima8on
•  Creates an anima8on by modifying an object's property values over a set
period of 8me
•  Folder res/animator
•  Too complicated to talk about this now. See you later.
Resources Type: The Rest
•  Not so important
•  But once you know the concept of Resource, you will be able to learn more
about another type of resource by yourself in a minute
ConfiguraHon Qualifier
•  ALERT: VERY IMPORTANT !!!
•  Very smart resource selec8on mechanic in Android
•  Folder format
<resources_name>-<qualifier>
•  Can be applied on any resource type
•  For example
res/drawable-xhdpi
res/layout-xxhdpi
ConfiguraHon Qualifier Type
•  dpi – ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi, nodpi, tvdpi
•  Screen size – small, normal, large, xlarge
•  Width – w<N>dp (for example, w820dp)
•  Smallest Width – sw<N>dp (for example, sw600dp)
•  Screen aspect – long, not long
•  PlaLorm version – v<N> (for example, v21)
•  And a lot more
•  So you might see something like drawable-notlong-land-xxxhdpi-xlarge-v21 …
•  Have to do it by order, cannot shuffle the posi8on
•  See more at
hpp://developer.android.com/guide/topics/resources/providing-
resources.html#Alterna8veResources
ConfiguraHon Qualifier: Select by qualifier
•  If hardware’s configura8on matches the qualifier, resource inside that
folder will be used
•  See an example
ConfiguraHon Qualifier
480x800 720x1200 960x1600
200x200
ConfiguraHon Qualifier: Auto Scale
•  But if the folder doesn’t existed, it will look into the nearest configura8on
and scale in the same way as dp !
•  For example, if image is decoded in xhdpi has 100 pixels width, when it is
decoded in mdpi, its width will be at 50 pixels
•  In the other words, if you put 50x50 pixels image in drawable-mdpi, you will
get the same size (but not quality) as you put 100x100 pixels image in
drawable-xhdpi
•  See an example
x2
x1
ConfiguraHon Qualifier: List of Qualifier
•  Very smart resource selec8on mechanic in Android
•  Folder format
<resources_name>-<qualifier>
•  For example
drawable-xhdpi
drawable-xxhdpi
Rule 15:
With knowledge about dp and
Configura8on Qualifier (and sure,
with some prac8cing), you will
80% win over Fragmenta8on !
Do we need to define every single qualifier?
NO and DON’T !
Best PracHces: Drawable
•  Put icon inside every single dpi qualifier in mipmap
•  Don’t put image files inside res/drawable and/or res/drawable-nodpi
unless you know what are you doing
•  res/drawable is treated like res/drawable-mdpi
•  Image put inside res/drawable-nodpi will be not scaled
•  Put Drawable Resources inside just a single folder and let it be scaled
automa8cally
•  res/drawble-xhdpi is recommended (just by me)
•  (Cont.) unless you really need to customize for some screen (and you will
need it)
•  Mobile/Tablet (because I will let you do)
•  Support for some high resolu8on screen (2K for example)
Best PracHces: Layout
•  Since dp is all the same in any single dpi, so you have no need to put
anything axer layout folder
•  Just use res/layout
•  Unless you need to customize the screen for Landscape Handset or for
Tablet
•  Use res/layout-land for Landscape
•  Use res/layout-sw600dp for Tablet screen
•  Use res/layout-sw600dp-land for Landscape Tablet screen
•  And it is mostly what we do for layout already …
Best PracHces: Layout
•  And you will need to do that as well … (separated Layout for Tablet)
Rule 16:
Be lean, don’t have too many
qualifiers. It causes more problem
rather than helping.
How to access resource from Java?: The Context
“Interface to global informa8on about an applica8on environment”
If you want to access to anything
-  Applica8on’s Resources
-  Launch Ac8vity
-  Send Broadcast
-  Receive Intent
-  Get Screen Resolu8on
-  A lot !!
* Context is one of the thing you will see the most in Android App Development, be used to it
How to access resource from Java?
•  context.findViewById(…)
•  context.getResources(…)
•  Curious why do we can call those methods without calling through context?
•  And what’s about setText(R.string.xxx) ?
… Play Play Play …
Rule 16:
Context is always be used
everywhere. Have a date with it
everyday from now on.

More Related Content

Similar to Chapter 9 - Resources System

Fernando F. Gallego - Efficient Android Resources 101
Fernando F. Gallego - Efficient Android Resources 101Fernando F. Gallego - Efficient Android Resources 101
Fernando F. Gallego - Efficient Android Resources 101Fernando Gallego
 
Designing Android apps for multiple screens
Designing Android apps for multiple screensDesigning Android apps for multiple screens
Designing Android apps for multiple screensAbhijeet Dutta
 
Beating Android Fragmentation, Brett Duncavage
Beating Android Fragmentation, Brett DuncavageBeating Android Fragmentation, Brett Duncavage
Beating Android Fragmentation, Brett DuncavageXamarin
 
Android Talks #3 Android Design Best Practices - for Designers and Developers
Android Talks #3 Android Design Best Practices - for Designers and DevelopersAndroid Talks #3 Android Design Best Practices - for Designers and Developers
Android Talks #3 Android Design Best Practices - for Designers and DevelopersDenis_infinum
 
Infinum Android Talks #03 - Android Design Best Practices - for Designers and...
Infinum Android Talks #03 - Android Design Best Practices - for Designers and...Infinum Android Talks #03 - Android Design Best Practices - for Designers and...
Infinum Android Talks #03 - Android Design Best Practices - for Designers and...Infinum
 
Android resource
Android resourceAndroid resource
Android resourceKrazy Koder
 
Android supporting multiple screen
Android supporting multiple screenAndroid supporting multiple screen
Android supporting multiple screenDao Quang Anh
 
Tehran's 2nd Android bootcamp
Tehran's 2nd Android bootcampTehran's 2nd Android bootcamp
Tehran's 2nd Android bootcampMohsen Mirhoseini
 
Android App Development 08 : Support Multiple Devices
Android App Development 08 : Support Multiple DevicesAndroid App Development 08 : Support Multiple Devices
Android App Development 08 : Support Multiple DevicesAnuchit Chalothorn
 
Android Programming Basic
Android Programming BasicAndroid Programming Basic
Android Programming BasicDuy Do Phan
 
Coding for different resolutions
Coding for different resolutionsCoding for different resolutions
Coding for different resolutionsRobin Srivastava
 
Android Effective UI: Tips, Tricks and Patterns
Android Effective UI: Tips, Tricks and PatternsAndroid Effective UI: Tips, Tricks and Patterns
Android Effective UI: Tips, Tricks and PatternsAdham Enaya
 
Android project architecture
Android project architectureAndroid project architecture
Android project architectureSourabh Sahu
 
Android Studio development model and.pptx
Android Studio development model and.pptxAndroid Studio development model and.pptx
Android Studio development model and.pptxVaibhavKhunger2
 
How to deal with Fragmentation on Android
How to deal with Fragmentation on AndroidHow to deal with Fragmentation on Android
How to deal with Fragmentation on AndroidSittiphol Phanvilai
 
Ts android supporting multiple screen
Ts   android supporting multiple screenTs   android supporting multiple screen
Ts android supporting multiple screenConfiz
 
Android Development
Android DevelopmentAndroid Development
Android Developmentmclougm4
 

Similar to Chapter 9 - Resources System (20)

Fernando F. Gallego - Efficient Android Resources 101
Fernando F. Gallego - Efficient Android Resources 101Fernando F. Gallego - Efficient Android Resources 101
Fernando F. Gallego - Efficient Android Resources 101
 
Android for Beginners
Android  for BeginnersAndroid  for Beginners
Android for Beginners
 
Designing Android apps for multiple screens
Designing Android apps for multiple screensDesigning Android apps for multiple screens
Designing Android apps for multiple screens
 
Beating Android Fragmentation, Brett Duncavage
Beating Android Fragmentation, Brett DuncavageBeating Android Fragmentation, Brett Duncavage
Beating Android Fragmentation, Brett Duncavage
 
Android Talks #3 Android Design Best Practices - for Designers and Developers
Android Talks #3 Android Design Best Practices - for Designers and DevelopersAndroid Talks #3 Android Design Best Practices - for Designers and Developers
Android Talks #3 Android Design Best Practices - for Designers and Developers
 
Infinum Android Talks #03 - Android Design Best Practices - for Designers and...
Infinum Android Talks #03 - Android Design Best Practices - for Designers and...Infinum Android Talks #03 - Android Design Best Practices - for Designers and...
Infinum Android Talks #03 - Android Design Best Practices - for Designers and...
 
UI and UX for Mobile Developers
UI and UX for Mobile DevelopersUI and UX for Mobile Developers
UI and UX for Mobile Developers
 
Android resource
Android resourceAndroid resource
Android resource
 
Android supporting multiple screen
Android supporting multiple screenAndroid supporting multiple screen
Android supporting multiple screen
 
Tehran's 2nd Android bootcamp
Tehran's 2nd Android bootcampTehran's 2nd Android bootcamp
Tehran's 2nd Android bootcamp
 
Android App Development 08 : Support Multiple Devices
Android App Development 08 : Support Multiple DevicesAndroid App Development 08 : Support Multiple Devices
Android App Development 08 : Support Multiple Devices
 
Android Programming Basic
Android Programming BasicAndroid Programming Basic
Android Programming Basic
 
Coding for different resolutions
Coding for different resolutionsCoding for different resolutions
Coding for different resolutions
 
Android Effective UI: Tips, Tricks and Patterns
Android Effective UI: Tips, Tricks and PatternsAndroid Effective UI: Tips, Tricks and Patterns
Android Effective UI: Tips, Tricks and Patterns
 
Lecture3
Lecture3Lecture3
Lecture3
 
Android project architecture
Android project architectureAndroid project architecture
Android project architecture
 
Android Studio development model and.pptx
Android Studio development model and.pptxAndroid Studio development model and.pptx
Android Studio development model and.pptx
 
How to deal with Fragmentation on Android
How to deal with Fragmentation on AndroidHow to deal with Fragmentation on Android
How to deal with Fragmentation on Android
 
Ts android supporting multiple screen
Ts   android supporting multiple screenTs   android supporting multiple screen
Ts android supporting multiple screen
 
Android Development
Android DevelopmentAndroid Development
Android Development
 

More from Sittiphol Phanvilai

More from Sittiphol Phanvilai (8)

Chapter 10 - Views Part 2
Chapter 10 - Views Part 2Chapter 10 - Views Part 2
Chapter 10 - Views Part 2
 
Chapter 6 - Views Part 1
Chapter 6 - Views Part 1Chapter 6 - Views Part 1
Chapter 6 - Views Part 1
 
Chapter 12 - Activity Intent
Chapter 12 - Activity  IntentChapter 12 - Activity  Intent
Chapter 12 - Activity Intent
 
Chapter 11 - Styling Theming
Chapter 11 - Styling ThemingChapter 11 - Styling Theming
Chapter 11 - Styling Theming
 
Chapter 4 - How to Think Like Android Developer Designer
Chapter 4  - How to Think Like Android Developer  DesignerChapter 4  - How to Think Like Android Developer  Designer
Chapter 4 - How to Think Like Android Developer Designer
 
Chapter 5 - Layouts
Chapter 5 - LayoutsChapter 5 - Layouts
Chapter 5 - Layouts
 
Chapter 7 - Debugging part 1
Chapter 7 - Debugging part 1Chapter 7 - Debugging part 1
Chapter 7 - Debugging part 1
 
Chapter 8 - Dimension Units
Chapter 8 - Dimension UnitsChapter 8 - Dimension Units
Chapter 8 - Dimension Units
 

Recently uploaded

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

Chapter 9 - Resources System

  • 2. What is Resources? It takes more than just code to build a great app. Resources are the addi8onal files and sta8c content that your code uses, such as bitmaps, layout defini8ons, user interface strings, anima8on instruc8ons, and more.
  • 3. What is Resources? A Storeroom contains pieces of data needed in applica8on •  Image (jpg, png) •  Layout (xml) •  etc.
  • 4. What is Resources? Java Source Codes Resources
  • 5. What is Resources? Source Code Resources Running Applica5on =+ + +
  • 6. What is Resources? How does Resources System look like? You should always externalize resources such as images and strings from your applica8on code, so that you can maintain them independently
  • 8. What is Resources? + It’s awesomeIt works
  • 9. Resources Type Drawable Something that can be drawn on screen Layout XML contains Layout hierarchy/ structure Menu Context menu, Popup menu Values Strings, Colors, Dimension, etc. Anima8on XML contains Property anima8on or View anima8on etc. Almost everything but Java source codes
  • 10. Resources Type: Drawable •  A drawable resource is a general concept for a graphic that can be drawn to the screen •  folder res/drawable •  Have A LOT to be learned, be prepared …
  • 12. How to refer resource from another resource? … Play Play Play …
  • 13. Rule 11: Each resource folder couldn’t have any child folder. Manage filename wisely.
  • 14. The R class: How are Resources compiled? Filename will be simply automa8cally turned into a int variable name under <package_name>.R.<resource_type> We call it “Resource ID” R.drawable.ic_launcher Ic_launcher.png Remember !
  • 15. Rule 12: Filename (ext not included) in resource folder will be turned into a variable name in Java. So you can’t use some character in the filename (for example, - +)
  • 16. Rule 13: Never has the same filename in the same folder although they are in the different file type (for example: icon1.jpg, icon1.png)
  • 17. Resources Type: Layout •  A layout resource defines the architecture for the UI in an Ac8vity or a component of a UI. •  Folder res/layout
  • 18. The R class: How are Resources compiled? #2 Every single resource will be put in the same place with different assigned Resource ID automa8cally set by Android compiler R.drawable.ic_launcher ac8vity_main.xml R.layout.ac8vity_main
  • 19. android:id •  Inside the layout xml file, you will find something like android:id=“@+id/xxxx” or android:id=“@id/xxxx” •  It is simply a Resource ID referring to a View inside a Layout •  You have no need to assign it for every single element, just one you need to reference
  • 20. Rule 14: Don’t use the same ID in the single file, but feel free to use in any other file
  • 21. Resources Type: Menu •  A menu resource defines an applica8on menu (Op8ons Menu, Context Menu, or submenu) that can be inflated with MenuInflater. •  Folder res/menu •  Learn more about Menus in Part 3
  • 22. Resources Type: Values •  Folder res/values •  Be careful, Resource ID is not R.values.xxxx •  Look into each one by one •  Dimension •  String •  Colors •  You will learn more by prac8ce all along this class
  • 23. Resources Type: Anim •  View Anima8on •  Creates an anima8on by performing a series of transforma8ons on a single image or creates an anima8on by showing a sequence of images in order •  Folder res/anim •  Too complicated to talk about this now. See you later.
  • 24. Resources Type: Animator •  Property Anima8on •  Creates an anima8on by modifying an object's property values over a set period of 8me •  Folder res/animator •  Too complicated to talk about this now. See you later.
  • 25. Resources Type: The Rest •  Not so important •  But once you know the concept of Resource, you will be able to learn more about another type of resource by yourself in a minute
  • 26. ConfiguraHon Qualifier •  ALERT: VERY IMPORTANT !!! •  Very smart resource selec8on mechanic in Android •  Folder format <resources_name>-<qualifier> •  Can be applied on any resource type •  For example res/drawable-xhdpi res/layout-xxhdpi
  • 27. ConfiguraHon Qualifier Type •  dpi – ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi, nodpi, tvdpi •  Screen size – small, normal, large, xlarge •  Width – w<N>dp (for example, w820dp) •  Smallest Width – sw<N>dp (for example, sw600dp) •  Screen aspect – long, not long •  PlaLorm version – v<N> (for example, v21) •  And a lot more •  So you might see something like drawable-notlong-land-xxxhdpi-xlarge-v21 … •  Have to do it by order, cannot shuffle the posi8on •  See more at hpp://developer.android.com/guide/topics/resources/providing- resources.html#Alterna8veResources
  • 28. ConfiguraHon Qualifier: Select by qualifier •  If hardware’s configura8on matches the qualifier, resource inside that folder will be used •  See an example
  • 30. ConfiguraHon Qualifier: Auto Scale •  But if the folder doesn’t existed, it will look into the nearest configura8on and scale in the same way as dp ! •  For example, if image is decoded in xhdpi has 100 pixels width, when it is decoded in mdpi, its width will be at 50 pixels •  In the other words, if you put 50x50 pixels image in drawable-mdpi, you will get the same size (but not quality) as you put 100x100 pixels image in drawable-xhdpi •  See an example x2 x1
  • 31. ConfiguraHon Qualifier: List of Qualifier •  Very smart resource selec8on mechanic in Android •  Folder format <resources_name>-<qualifier> •  For example drawable-xhdpi drawable-xxhdpi
  • 32. Rule 15: With knowledge about dp and Configura8on Qualifier (and sure, with some prac8cing), you will 80% win over Fragmenta8on !
  • 33. Do we need to define every single qualifier? NO and DON’T !
  • 34. Best PracHces: Drawable •  Put icon inside every single dpi qualifier in mipmap •  Don’t put image files inside res/drawable and/or res/drawable-nodpi unless you know what are you doing •  res/drawable is treated like res/drawable-mdpi •  Image put inside res/drawable-nodpi will be not scaled •  Put Drawable Resources inside just a single folder and let it be scaled automa8cally •  res/drawble-xhdpi is recommended (just by me) •  (Cont.) unless you really need to customize for some screen (and you will need it) •  Mobile/Tablet (because I will let you do) •  Support for some high resolu8on screen (2K for example)
  • 35. Best PracHces: Layout •  Since dp is all the same in any single dpi, so you have no need to put anything axer layout folder •  Just use res/layout •  Unless you need to customize the screen for Landscape Handset or for Tablet •  Use res/layout-land for Landscape •  Use res/layout-sw600dp for Tablet screen •  Use res/layout-sw600dp-land for Landscape Tablet screen •  And it is mostly what we do for layout already …
  • 36. Best PracHces: Layout •  And you will need to do that as well … (separated Layout for Tablet)
  • 37. Rule 16: Be lean, don’t have too many qualifiers. It causes more problem rather than helping.
  • 38. How to access resource from Java?: The Context “Interface to global informa8on about an applica8on environment” If you want to access to anything -  Applica8on’s Resources -  Launch Ac8vity -  Send Broadcast -  Receive Intent -  Get Screen Resolu8on -  A lot !! * Context is one of the thing you will see the most in Android App Development, be used to it
  • 39. How to access resource from Java? •  context.findViewById(…) •  context.getResources(…) •  Curious why do we can call those methods without calling through context? •  And what’s about setText(R.string.xxx) ? … Play Play Play …
  • 40. Rule 16: Context is always be used everywhere. Have a date with it everyday from now on.