SlideShare a Scribd company logo
1 of 19
Android resources
Ilio Catallo, Eleonora Ciceri – Politecnico di Milano
ilio.catallo@polimi.it, eleonora.ciceri@polimi.it
1
Motivation
¤ It’s always good practice to keep non-code resources,
e.g., images and string constants, external to your code
2
Upsides
¤ By externalizing resources, you make them easier to
maintain, update and manage
¤ Example: support a new screen size
¤ Since resources are externalized, it is possible to alter the
code without modifying the resources (and viceversa)
¤ Android automatically selects the correct resources
without you having to write code
¤ Example: select the proper language
3
Creating resources
¤ Application resources are stored
under the res/ folder:
¤ Grouped by resource type
¤ Stored in subfolders (one for
each resource type)
¤ When your application is built, these
resources will be compiled and
compressed as efficiently as possible
and included in your application
package
4
Creating resources
¤ Android supports the externalization of:
¤ Simple values, such as strings and colors
¤ Complex values
¤ Images (drawables)
¤ Animations
¤ UI elements
¤ Themes
¤ Menus
¤ Layouts
5
Creating resources
¤ Android supports the externalization of:
¤ Simple values, such as strings and colors
¤ Complex values
¤ Images (drawables)
¤ Animations
¤ UI elements
¤ Themes
¤ Menus
¤ Layouts
6
Dealing with different screen sizes
¤ Units of measurements:
¤ You must avoid using absolute pixels to define distances or sizes,
as different screens have different pixel densities
7
dp Density-independent pixel. 1 dp is equivalent to one
pixel on a 160 dpi screen
sp Scale-independent pixel. An sp is the same base unit
as dp, but it is (possibly) scaled by the user’s preferred
text size. It should be used when defining text size
pt Point. A point is defined to be 1/72 of an inch
px Pixel. Corresponds to actual pixels on the screen
Dealingwith
differentscreensizes
8
¤ Screen dimension
¤ 4 inches (diagonally)
¤ Screen width
¤ 2.04 inches
¤ Resolution
¤ 480(width) x 800(height)
¤ Pixel density
¤ 480/2.04 = 235 dpi
Dealing with different screen sizes
¤ Android defines and recognizes four screen densities
¤ Medium density: mdpi, 160 dpi
¤ High density: hdpi, 240 dpi
¤ Extra high density: xhdpi, 320 dpi
¤ Extra extra high density: xxhdpi, 480 dpi
¤ In the Android terminology, screen densities are called
density buckets
9
Provide alternative bitmaps
¤ You should always provide bitmap resources tailored to
each of the available density bucket
¤ To do so, each single image should be provided at the
following scales:
¤ Normal density (mdpi): 1.0x
¤ High density (hdpi): 1.5x
¤ Extra-high density (xhdpi): 2.0x
¤ Extra extra high density (xxhdpi): 3.0x
10
Example
¤ Example: the same image must be generated at
¤ 100x100 for mdpi
¤ 150x150 for hdpi,
¤ 200x200 for xhdpi
¤ 300x300 for xxhdpi
11
Scaling the same image
12
mdpi
hdpi
xhdpi
xxhdpi
Drawables
¤ Each density bucket has an associated subfolder in res/
¤ Namely, such subfolders are:
¤ res/drawable-mdpi
¤ res/drawable-hdpi
¤ res/drawable-xhdpi
¤ res/drawable-xxhdpi
¤ The preferred format for a bitmap resource is PNG, but
JPG and GIF images are also supported
13
Scaling the same image
14
res/drawable-mdpi
res/drawable-hdpi
res/drawable-xhdpi
res/drawable-xxhdpi
Simple values:
string resources
¤ Strings, String arrays and plurals are stored in
/res/values/strings.xml
15
<resources>
<string name="app_name">TakeNotes</string>
<string name="action_settings">Settings</string>
</resources>
[...] android:label="@string/app_name" [...]
¤ If you remember, they were used in AndroidManifest.xml:
Simple values:
string resources
¤ String resources are specified with the string tag
¤ Font formatting: it is possible to format strings by using
HTML tags (e.g., <b> or <i>)
¤ A string array defines an array of strings
16
<string name="stop_message">Stop</string>
<string-array name="string-array”>
<item>Item1</item>
<item>Item2</item>
</string-array>
Simple values:
color resources
¤ Color profiles are stored in /res/values/colors.xml
¤ Use the color tag to define a new color resource
¤ A color is specified using a # symbol followed by the
alpha channel
¤ Examples:
¤ #RRGGBB (RGB values)
¤ #AARRGGBB (RGB values + alpha channel)
17
<color name="opaque-blue">#CD43A2</color>
References
18
References
¤ Android Training, Supporting different screen sizes
http://developer.android.com/training/multiscreen/scree
nsizes.html
¤ Android Training, Supporting different densities
http://developer.android.com/training/multiscreen/scree
ndensities.html
19

More Related Content

What's hot

Android application structure
Android application structureAndroid application structure
Android application structure
Alexey Ustenko
 
Android animation
Android animationAndroid animation
Android animation
Krazy Koder
 

What's hot (20)

Android UI
Android UIAndroid UI
Android UI
 
Activity lifecycle
Activity lifecycleActivity lifecycle
Activity lifecycle
 
Android chapter02-setup2-emulator
Android chapter02-setup2-emulatorAndroid chapter02-setup2-emulator
Android chapter02-setup2-emulator
 
Android application structure
Android application structureAndroid application structure
Android application structure
 
04 activities and activity life cycle
04 activities and activity life cycle04 activities and activity life cycle
04 activities and activity life cycle
 
Activities, Fragments, and Events
Activities, Fragments, and EventsActivities, Fragments, and Events
Activities, Fragments, and Events
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android Studio
 
Supporting multiple screens on android
Supporting multiple screens on androidSupporting multiple screens on android
Supporting multiple screens on android
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studio
 
Android animation
Android animationAndroid animation
Android animation
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
 
Android Architecture
Android ArchitectureAndroid Architecture
Android Architecture
 
Android - Application Framework
Android - Application FrameworkAndroid - Application Framework
Android - Application Framework
 
Android structure
Android structureAndroid structure
Android structure
 
Android Application Development
Android Application DevelopmentAndroid Application Development
Android Application Development
 
Introduction to Eclipse IDE
Introduction to Eclipse IDEIntroduction to Eclipse IDE
Introduction to Eclipse IDE
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming Basics
 
Android Application Development Using Java
Android Application Development Using JavaAndroid Application Development Using Java
Android Application Development Using Java
 
Android Fragment
Android FragmentAndroid Fragment
Android Fragment
 
Android Location and Maps
Android Location and MapsAndroid Location and Maps
Android Location and Maps
 

Similar to Android resources

Ts android supporting multiple screen
Ts   android supporting multiple screenTs   android supporting multiple screen
Ts android supporting multiple screen
Confiz
 
Technical glossary
Technical glossaryTechnical glossary
Technical glossary
halo4robo
 
Technical file powerpoint
Technical file powerpointTechnical file powerpoint
Technical file powerpoint
Maguire94
 
Technical glossary
Technical glossaryTechnical glossary
Technical glossary
halo4robo
 
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
Anuchit Chalothorn
 

Similar to Android resources (20)

Chapter 9 - Resources System
Chapter 9 - Resources SystemChapter 9 - Resources System
Chapter 9 - Resources System
 
Designing Android apps for multiple screens
Designing Android apps for multiple screensDesigning Android apps for multiple screens
Designing Android apps for multiple screens
 
Android supporting multiple screen
Android supporting multiple screenAndroid supporting multiple screen
Android supporting multiple screen
 
Android training day 3
Android training day 3Android training day 3
Android training day 3
 
Ts android supporting multiple screen
Ts   android supporting multiple screenTs   android supporting multiple screen
Ts android supporting multiple screen
 
Android App Development - 03 Resources
Android App Development - 03 ResourcesAndroid App Development - 03 Resources
Android App Development - 03 Resources
 
Technical glossary
Technical glossaryTechnical glossary
Technical glossary
 
Supporting Multiple Screen In Android
Supporting Multiple Screen In AndroidSupporting Multiple Screen In Android
Supporting Multiple Screen In Android
 
UI and UX for Mobile Developers
UI and UX for Mobile DevelopersUI and UX for Mobile Developers
UI and UX for Mobile Developers
 
Technical file powerpoint
Technical file powerpointTechnical file powerpoint
Technical file powerpoint
 
divide and qonquer
divide and qonquerdivide and qonquer
divide and qonquer
 
Technical glossary
Technical glossaryTechnical glossary
Technical glossary
 
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
 
Etsy - Android & Design
Etsy - Android & DesignEtsy - Android & Design
Etsy - Android & Design
 
Supporting multi screen in android
Supporting multi screen in androidSupporting multi screen in android
Supporting multi screen in android
 
Deep Dive Xamarin.Android
Deep Dive Xamarin.AndroidDeep Dive Xamarin.Android
Deep Dive Xamarin.Android
 
Android for Beginners
Android  for BeginnersAndroid  for Beginners
Android for Beginners
 
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
 
Information Processes and Technology HSC
Information Processes and Technology HSCInformation Processes and Technology HSC
Information Processes and Technology HSC
 
Responsive Web Design tips and tricks.
Responsive Web Design tips and tricks.Responsive Web Design tips and tricks.
Responsive Web Design tips and tricks.
 

More from ma-polimi

More from ma-polimi (6)

Broadcast Receivers in Android
Broadcast Receivers in AndroidBroadcast Receivers in Android
Broadcast Receivers in Android
 
Persistence in Android
Persistence in AndroidPersistence in Android
Persistence in Android
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Android
 
Android Components & Manifest
Android Components & ManifestAndroid Components & Manifest
Android Components & Manifest
 
TakeNotes
TakeNotesTakeNotes
TakeNotes
 
Introduction To Android
Introduction To AndroidIntroduction To Android
Introduction To Android
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Recently uploaded (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 

Android resources

  • 1. Android resources Ilio Catallo, Eleonora Ciceri – Politecnico di Milano ilio.catallo@polimi.it, eleonora.ciceri@polimi.it 1
  • 2. Motivation ¤ It’s always good practice to keep non-code resources, e.g., images and string constants, external to your code 2
  • 3. Upsides ¤ By externalizing resources, you make them easier to maintain, update and manage ¤ Example: support a new screen size ¤ Since resources are externalized, it is possible to alter the code without modifying the resources (and viceversa) ¤ Android automatically selects the correct resources without you having to write code ¤ Example: select the proper language 3
  • 4. Creating resources ¤ Application resources are stored under the res/ folder: ¤ Grouped by resource type ¤ Stored in subfolders (one for each resource type) ¤ When your application is built, these resources will be compiled and compressed as efficiently as possible and included in your application package 4
  • 5. Creating resources ¤ Android supports the externalization of: ¤ Simple values, such as strings and colors ¤ Complex values ¤ Images (drawables) ¤ Animations ¤ UI elements ¤ Themes ¤ Menus ¤ Layouts 5
  • 6. Creating resources ¤ Android supports the externalization of: ¤ Simple values, such as strings and colors ¤ Complex values ¤ Images (drawables) ¤ Animations ¤ UI elements ¤ Themes ¤ Menus ¤ Layouts 6
  • 7. Dealing with different screen sizes ¤ Units of measurements: ¤ You must avoid using absolute pixels to define distances or sizes, as different screens have different pixel densities 7 dp Density-independent pixel. 1 dp is equivalent to one pixel on a 160 dpi screen sp Scale-independent pixel. An sp is the same base unit as dp, but it is (possibly) scaled by the user’s preferred text size. It should be used when defining text size pt Point. A point is defined to be 1/72 of an inch px Pixel. Corresponds to actual pixels on the screen
  • 8. Dealingwith differentscreensizes 8 ¤ Screen dimension ¤ 4 inches (diagonally) ¤ Screen width ¤ 2.04 inches ¤ Resolution ¤ 480(width) x 800(height) ¤ Pixel density ¤ 480/2.04 = 235 dpi
  • 9. Dealing with different screen sizes ¤ Android defines and recognizes four screen densities ¤ Medium density: mdpi, 160 dpi ¤ High density: hdpi, 240 dpi ¤ Extra high density: xhdpi, 320 dpi ¤ Extra extra high density: xxhdpi, 480 dpi ¤ In the Android terminology, screen densities are called density buckets 9
  • 10. Provide alternative bitmaps ¤ You should always provide bitmap resources tailored to each of the available density bucket ¤ To do so, each single image should be provided at the following scales: ¤ Normal density (mdpi): 1.0x ¤ High density (hdpi): 1.5x ¤ Extra-high density (xhdpi): 2.0x ¤ Extra extra high density (xxhdpi): 3.0x 10
  • 11. Example ¤ Example: the same image must be generated at ¤ 100x100 for mdpi ¤ 150x150 for hdpi, ¤ 200x200 for xhdpi ¤ 300x300 for xxhdpi 11
  • 12. Scaling the same image 12 mdpi hdpi xhdpi xxhdpi
  • 13. Drawables ¤ Each density bucket has an associated subfolder in res/ ¤ Namely, such subfolders are: ¤ res/drawable-mdpi ¤ res/drawable-hdpi ¤ res/drawable-xhdpi ¤ res/drawable-xxhdpi ¤ The preferred format for a bitmap resource is PNG, but JPG and GIF images are also supported 13
  • 14. Scaling the same image 14 res/drawable-mdpi res/drawable-hdpi res/drawable-xhdpi res/drawable-xxhdpi
  • 15. Simple values: string resources ¤ Strings, String arrays and plurals are stored in /res/values/strings.xml 15 <resources> <string name="app_name">TakeNotes</string> <string name="action_settings">Settings</string> </resources> [...] android:label="@string/app_name" [...] ¤ If you remember, they were used in AndroidManifest.xml:
  • 16. Simple values: string resources ¤ String resources are specified with the string tag ¤ Font formatting: it is possible to format strings by using HTML tags (e.g., <b> or <i>) ¤ A string array defines an array of strings 16 <string name="stop_message">Stop</string> <string-array name="string-array”> <item>Item1</item> <item>Item2</item> </string-array>
  • 17. Simple values: color resources ¤ Color profiles are stored in /res/values/colors.xml ¤ Use the color tag to define a new color resource ¤ A color is specified using a # symbol followed by the alpha channel ¤ Examples: ¤ #RRGGBB (RGB values) ¤ #AARRGGBB (RGB values + alpha channel) 17 <color name="opaque-blue">#CD43A2</color>
  • 19. References ¤ Android Training, Supporting different screen sizes http://developer.android.com/training/multiscreen/scree nsizes.html ¤ Android Training, Supporting different densities http://developer.android.com/training/multiscreen/scree ndensities.html 19