SlideShare a Scribd company logo
Application Resources
Topics
• Externalizing resources
• Default vs. Alternative resources
• Providing resources
• Providing alternative resources
• Accessing resources
> in Code
> in XML
• Localization
Externalizing Resources
What are Resources?
• Any static data that can be externalized from
code
> Layouts
> Images
> Video and audio files
> Strings
> Menu definitions
> Animation
> etc.
Why Externalizing Resources?
• Allows you to maintain them independently
(from code)
• Allows you to provide alternative resources that
support specific device configurations such as
different languages or screen sizes, which
becomes increasingly important as more
Android-powered devices become available
with different configurations.
Default vs. Alternative
Resources
Default vs. Alternative Resources
• For any type of resource, you can specify
default and multiple alternative resources for
your application
• Default resources are those that should be used
regardless of the device configuration or when
there are no alternative resources that match
the current configuration
• Alternative resources are those that you've
designed for use with a specific configuration.
Default vs. Alternative Resources
• Two device configurations, both using default resources
• Two device configurations, one using alternative
resources.
Providing Resources
Grouping Resources under /res directory
• You should place each type of resource in a
specific subdirectory of your project's res/
directory. For example, here's the file hierarchy
for a simple project:
Myproject/
src/
MyActivity.java
res/
drawable/
icon.png
layout/
main.xml
info.xml
values/
strings.xml
Resources Directories
• anim/
> XML files that define tween animations
• color/
• drawable/
• layout/
• menu/
• raw/
• values/
• xml/
Providing Alternative
Resources
Why Alternative Resources?
• Almost every application should provide
alternative resources to support specific device
configurations.
> For instance, you should include alternative drawable
resources for different screen densities and
alternative string resources for different languages.
• At runtime, Android automatically detects the
current device configuration and loads the
appropriate resources
How to specify Alternative Resources?
• Create a new directory in res/ named in the
form <resources_name>-<config_qualifier>
> <resources_name> is the directory name of the
corresponding default resources
> <config_qualifier> is a name that specifies a
configuration for which these resources are to be
used
• Save your alternative resources in this new
directory
> The resource files must be named exactly the same
as the default resource files.
Example: Default & Alternative Resources
res/
drawable/
icon.png
background.png
drawable-hdpi/
icon.png
background.png
• The hdpi qualifier indicates that the resources in that
directory are for devices with a high-density screen.
• While the images in each drawable directory are
sized for a specific screen density, the filenames are
the same.
Alternative Resource Qualifier Names
• Mobile country code (MCC)
> mcc310, mcc310-mnc004
• Language and region
> en, fr, en-rUS, fr-rFR, fr-rCA
• Screen size
> small, normal, large
• Screen orientation
> port, land
• Screen density
> ldpi, mdpi, hdpi, nodpi
How Android Finds Best-matching Resource
Accessing Resources
Resource ID
• Once you provide a resource in your application
(discussed in Providing Resources), you can
apply it by referencing its resource ID.
• All resource IDs are defined in your project's R
class, which the aapt tool automatically
generates.
• For each type of resource, there is an R
subclass (for example, R.drawable for all
drawable resources) and for each resource of
that type, there is a static integer (for example,
R.drawable.icon). This integer is the resource ID
that you can use to retrieve your resource.
Two ways to access a resource
• In code: Using an static integer from a sub-class
of your R class, such as:
> R.string.hello (string is the resource type and hello is
the resource name)
• In XML: Using a special XML syntax that also
corresponds to the resource ID defined in your
R class, such as:
> @string/hello (string is the resource type and hello is
the resource name)
Accessing Resources:
In Code
Accessing Resources In Code
• You can use a resource in code by passing the
resource ID as a method parameter.
• For example, you can set an ImageView to use
the res/drawable/myimage.png resource using
setImageResource():
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);
Example: Accessing Resources in Code
// Load a background for the current screen from a drawable resource
getWindow().setBackgroundDrawableResource(R.drawable.my_background_image) ;
// Set the Activity title by getting a string from the Resources object, because
// this method requires a CharSequence rather than a resource ID
getWindow().setTitle(getResources().getText(R.string.main_title));
// Load a custom layout for the current screen
setContentView(R.layout.main_screen);
// Set a slide in animation by getting an Animation from the Resources object
mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.hyperspace_in));
// Set the text on a TextView object using a resource ID
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello_message);
Accessing Platform Resources
• Android contains a number of standard resources,
such as styles, themes, and layouts. To access these
resource, qualify your resource reference with the
android package name.
// Android provides a layout resource you can use for list items in a ListAdapter.
// In this example, simple_list_item_1 is a layout resource defined by the
// platform for items in a ListView. You can use this instead of creating your
// own layout for list items.
setListAdapter(
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
myarray));
Accessing Resources:
In XML
Accessing Resources from XML
• You can define values for some XML attributes
and elements using a reference to an existing
resource.
• You will often do this when creating layout files,
to supply strings and images for your widgets.
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/submit" />
Example: Accessing Resources from XML
• If you have the following resource file that includes a
color resource and a string resource:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<string name="hello">Hello!</string>
</resources>
• You can use these resources in the following layout
file to set the text color and text string:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/opaque_red"
android:text="@string/hello" />
Example: Accessing Resources from XML
• To reference a system resource, you would need to
include the package name.
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@android:color/secondary_text_dark"
android:text="@string/hello" />
Thank you
Ad

Recommended

SQLITE Android
SQLITE Android
Sourabh Sahu
 
Android UI
Android UI
nationalmobileapps
 
Data Storage In Android
Data Storage In Android
Aakash Ugale
 
Android resources
Android resources
ma-polimi
 
Android datastorage
Android datastorage
Krazy Koder
 
android activity
android activity
Deepa Rani
 
Android activity lifecycle
Android activity lifecycle
Soham Patel
 
android menus
android menus
Deepa Rani
 
Android Widget
Android Widget
ELLURU Kalyan
 
Android intents
Android intents
Siva Ramakrishna kv
 
Android Location and Maps
Android Location and Maps
Jussi Pohjolainen
 
Content provider in_android
Content provider in_android
PRITI TELMORE
 
android layouts
android layouts
Deepa Rani
 
SQLite database in android
SQLite database in android
Gourav Kumar Saini
 
JAVA AWT
JAVA AWT
shanmuga rajan
 
Creating the first app with android studio
Creating the first app with android studio
Parinita03
 
Fragment
Fragment
nationalmobileapps
 
Android - Android Intent Types
Android - Android Intent Types
Vibrant Technologies & Computers
 
Windowforms controls c#
Windowforms controls c#
prabhu rajendran
 
Android User Interface
Android User Interface
Shakib Hasan Sumon
 
Broadcast Receiver
Broadcast Receiver
nationalmobileapps
 
Layouts in android
Layouts in android
Durai S
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast Receivers
CodeAndroid
 
Android Services
Android Services
Ahsanul Karim
 
Android Intent.pptx
Android Intent.pptx
vishal choudhary
 
Android activity
Android activity
Krazy Koder
 
Android Basic Components
Android Basic Components
Jussi Pohjolainen
 
Android styles and themes
Android styles and themes
Sourabh Sahu
 
Android Resource Manager
Android Resource Manager
Sandeep Marathe
 
Droidcon it 2014 best practices to develop for different android device class...
Droidcon it 2014 best practices to develop for different android device class...
Hasan Hosgel
 

More Related Content

What's hot (20)

Android Widget
Android Widget
ELLURU Kalyan
 
Android intents
Android intents
Siva Ramakrishna kv
 
Android Location and Maps
Android Location and Maps
Jussi Pohjolainen
 
Content provider in_android
Content provider in_android
PRITI TELMORE
 
android layouts
android layouts
Deepa Rani
 
SQLite database in android
SQLite database in android
Gourav Kumar Saini
 
JAVA AWT
JAVA AWT
shanmuga rajan
 
Creating the first app with android studio
Creating the first app with android studio
Parinita03
 
Fragment
Fragment
nationalmobileapps
 
Android - Android Intent Types
Android - Android Intent Types
Vibrant Technologies & Computers
 
Windowforms controls c#
Windowforms controls c#
prabhu rajendran
 
Android User Interface
Android User Interface
Shakib Hasan Sumon
 
Broadcast Receiver
Broadcast Receiver
nationalmobileapps
 
Layouts in android
Layouts in android
Durai S
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast Receivers
CodeAndroid
 
Android Services
Android Services
Ahsanul Karim
 
Android Intent.pptx
Android Intent.pptx
vishal choudhary
 
Android activity
Android activity
Krazy Koder
 
Android Basic Components
Android Basic Components
Jussi Pohjolainen
 
Android styles and themes
Android styles and themes
Sourabh Sahu
 

Viewers also liked (11)

Android Resource Manager
Android Resource Manager
Sandeep Marathe
 
Droidcon it 2014 best practices to develop for different android device class...
Droidcon it 2014 best practices to develop for different android device class...
Hasan Hosgel
 
Fernando F. Gallego - Efficient Android Resources 101
Fernando F. Gallego - Efficient Android Resources 101
Fernando Gallego
 
Android Basic Tutorial
Android Basic Tutorial
Smartmonk
 
Android application structure
Android application structure
Alexey Ustenko
 
Android ppt
Android ppt
Tarun Bamba
 
Android graphics
Android graphics
Krazy Koder
 
Android complete basic Guide
Android complete basic Guide
AKASH SINGH
 
Introduction to Android Fragments
Introduction to Android Fragments
Sergi Martínez
 
Android Development: The Basics
Android Development: The Basics
Mike Desjardins
 
Android ppt
Android ppt
srikanth982
 
Android Resource Manager
Android Resource Manager
Sandeep Marathe
 
Droidcon it 2014 best practices to develop for different android device class...
Droidcon it 2014 best practices to develop for different android device class...
Hasan Hosgel
 
Fernando F. Gallego - Efficient Android Resources 101
Fernando F. Gallego - Efficient Android Resources 101
Fernando Gallego
 
Android Basic Tutorial
Android Basic Tutorial
Smartmonk
 
Android application structure
Android application structure
Alexey Ustenko
 
Android graphics
Android graphics
Krazy Koder
 
Android complete basic Guide
Android complete basic Guide
AKASH SINGH
 
Introduction to Android Fragments
Introduction to Android Fragments
Sergi Martínez
 
Android Development: The Basics
Android Development: The Basics
Mike Desjardins
 
Ad

Similar to Android resource (20)

eyeryeryeryeryeyeyeryeryyerye13872085.ppt
eyeryeryeryeryeyeyeryeryyerye13872085.ppt
FarhanGhafoor7
 
Android App Development - 03 Resources
Android App Development - 03 Resources
Diego Grancini
 
Android App Development 08 : Support Multiple Devices
Android App Development 08 : Support Multiple Devices
Anuchit Chalothorn
 
Android Resources.docx
Android Resources.docx
KNANTHINIMCA
 
03 android application structure
03 android application structure
Sokngim Sa
 
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
NicheTech Com. Solutions Pvt. Ltd.
 
Globalization and accessibility
Globalization and accessibility
aspnet123
 
Chapter 9 - Resources System
Chapter 9 - Resources System
Sittiphol Phanvilai
 
Android structure
Android structure
Kumar
 
Android Application Fundamentals
Android Application Fundamentals
Vikalp Jain
 
Lesson 09 Resources and Settings in WPF
Lesson 09 Resources and Settings in WPF
Quang Nguyễn Bá
 
Android project architecture
Android project architecture
Sourabh Sahu
 
Android stepbystep
Android stepbystep
Krazy Koder
 
Androidify workshop
Androidify workshop
Alexey Buzdin
 
Consistent UI Across Android Devices
Consistent UI Across Android Devices
Irene Duke
 
Android resources in android-chapter9
Android resources in android-chapter9
Dr. Ramkumar Lakshminarayanan
 
Introduction+to+R.pdf
Introduction+to+R.pdf
MudasserAziz2
 
Localization and Shared Preferences in android
Localization and Shared Preferences in android
Aly Arman
 
Android dev tips
Android dev tips
Kanda Runapongsa Saikaew
 
Anatomy of android aplication
Anatomy of android aplication
poojapainter
 
eyeryeryeryeryeyeyeryeryyerye13872085.ppt
eyeryeryeryeryeyeyeryeryyerye13872085.ppt
FarhanGhafoor7
 
Android App Development - 03 Resources
Android App Development - 03 Resources
Diego Grancini
 
Android App Development 08 : Support Multiple Devices
Android App Development 08 : Support Multiple Devices
Anuchit Chalothorn
 
Android Resources.docx
Android Resources.docx
KNANTHINIMCA
 
03 android application structure
03 android application structure
Sokngim Sa
 
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
NicheTech Com. Solutions Pvt. Ltd.
 
Globalization and accessibility
Globalization and accessibility
aspnet123
 
Android structure
Android structure
Kumar
 
Android Application Fundamentals
Android Application Fundamentals
Vikalp Jain
 
Lesson 09 Resources and Settings in WPF
Lesson 09 Resources and Settings in WPF
Quang Nguyễn Bá
 
Android project architecture
Android project architecture
Sourabh Sahu
 
Android stepbystep
Android stepbystep
Krazy Koder
 
Consistent UI Across Android Devices
Consistent UI Across Android Devices
Irene Duke
 
Introduction+to+R.pdf
Introduction+to+R.pdf
MudasserAziz2
 
Localization and Shared Preferences in android
Localization and Shared Preferences in android
Aly Arman
 
Anatomy of android aplication
Anatomy of android aplication
poojapainter
 
Ad

More from Krazy Koder (20)

2310 b xd
2310 b xd
Krazy Koder
 
2310 b xd
2310 b xd
Krazy Koder
 
2310 b xd
2310 b xd
Krazy Koder
 
2310 b xc
2310 b xc
Krazy Koder
 
2310 b xb
2310 b xb
Krazy Koder
 
2310 b 17
2310 b 17
Krazy Koder
 
2310 b 16
2310 b 16
Krazy Koder
 
2310 b 16
2310 b 16
Krazy Koder
 
2310 b 15
2310 b 15
Krazy Koder
 
2310 b 15
2310 b 15
Krazy Koder
 
2310 b 14
2310 b 14
Krazy Koder
 
2310 b 13
2310 b 13
Krazy Koder
 
2310 b 12
2310 b 12
Krazy Koder
 
2310 b 11
2310 b 11
Krazy Koder
 
2310 b 10
2310 b 10
Krazy Koder
 
2310 b 09
2310 b 09
Krazy Koder
 
2310 b 08
2310 b 08
Krazy Koder
 
2310 b 08
2310 b 08
Krazy Koder
 
2310 b 08
2310 b 08
Krazy Koder
 
2310 b 07
2310 b 07
Krazy Koder
 

Recently uploaded (20)

English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
LDMMIA Practitioner Student Reiki Yoga S2 Video PDF Without Yogi Goddess
LDMMIA Practitioner Student Reiki Yoga S2 Video PDF Without Yogi Goddess
LDM & Mia eStudios
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Nutrition Assessment and Nutrition Education – Unit 4 | B.Sc Nursing 5th Seme...
Nutrition Assessment and Nutrition Education – Unit 4 | B.Sc Nursing 5th Seme...
RAKESH SAJJAN
 
VCE Literature Section A Exam Response Guide
VCE Literature Section A Exam Response Guide
jpinnuck
 
Environmental Science, Environmental Health, and Sanitation – Unit 3 | B.Sc N...
Environmental Science, Environmental Health, and Sanitation – Unit 3 | B.Sc N...
RAKESH SAJJAN
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
“THE BEST CLASS IN SCHOOL”. _
“THE BEST CLASS IN SCHOOL”. _
Colégio Santa Teresinha
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 
English 3 Quarter 1_LEwithLAS_Week 1.pdf
English 3 Quarter 1_LEwithLAS_Week 1.pdf
DeAsisAlyanajaneH
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
LDMMIA Practitioner Student Reiki Yoga S2 Video PDF Without Yogi Goddess
LDMMIA Practitioner Student Reiki Yoga S2 Video PDF Without Yogi Goddess
LDM & Mia eStudios
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Battle of Bookworms 2025 - U25 Literature Quiz by Pragya
Pragya - UEM Kolkata Quiz Club
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
University of Ghana Cracks Down on Misconduct: Over 100 Students Sanctioned
Kweku Zurek
 
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
ROLE PLAY: FIRST AID -CPR & RECOVERY POSITION.pptx
Belicia R.S
 
Nutrition Assessment and Nutrition Education – Unit 4 | B.Sc Nursing 5th Seme...
Nutrition Assessment and Nutrition Education – Unit 4 | B.Sc Nursing 5th Seme...
RAKESH SAJJAN
 
VCE Literature Section A Exam Response Guide
VCE Literature Section A Exam Response Guide
jpinnuck
 
Environmental Science, Environmental Health, and Sanitation – Unit 3 | B.Sc N...
Environmental Science, Environmental Health, and Sanitation – Unit 3 | B.Sc N...
RAKESH SAJJAN
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
Tanja Vujicic - PISA for Schools contact Info
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Paper 107 | From Watchdog to Lapdog: Ishiguro’s Fiction and the Rise of “Godi...
Rajdeep Bavaliya
 
This is why students from these 44 institutions have not received National Se...
This is why students from these 44 institutions have not received National Se...
Kweku Zurek
 

Android resource

  • 2. Topics • Externalizing resources • Default vs. Alternative resources • Providing resources • Providing alternative resources • Accessing resources > in Code > in XML • Localization
  • 4. What are Resources? • Any static data that can be externalized from code > Layouts > Images > Video and audio files > Strings > Menu definitions > Animation > etc.
  • 5. Why Externalizing Resources? • Allows you to maintain them independently (from code) • Allows you to provide alternative resources that support specific device configurations such as different languages or screen sizes, which becomes increasingly important as more Android-powered devices become available with different configurations.
  • 7. Default vs. Alternative Resources • For any type of resource, you can specify default and multiple alternative resources for your application • Default resources are those that should be used regardless of the device configuration or when there are no alternative resources that match the current configuration • Alternative resources are those that you've designed for use with a specific configuration.
  • 8. Default vs. Alternative Resources • Two device configurations, both using default resources • Two device configurations, one using alternative resources.
  • 10. Grouping Resources under /res directory • You should place each type of resource in a specific subdirectory of your project's res/ directory. For example, here's the file hierarchy for a simple project: Myproject/ src/ MyActivity.java res/ drawable/ icon.png layout/ main.xml info.xml values/ strings.xml
  • 11. Resources Directories • anim/ > XML files that define tween animations • color/ • drawable/ • layout/ • menu/ • raw/ • values/ • xml/
  • 13. Why Alternative Resources? • Almost every application should provide alternative resources to support specific device configurations. > For instance, you should include alternative drawable resources for different screen densities and alternative string resources for different languages. • At runtime, Android automatically detects the current device configuration and loads the appropriate resources
  • 14. How to specify Alternative Resources? • Create a new directory in res/ named in the form <resources_name>-<config_qualifier> > <resources_name> is the directory name of the corresponding default resources > <config_qualifier> is a name that specifies a configuration for which these resources are to be used • Save your alternative resources in this new directory > The resource files must be named exactly the same as the default resource files.
  • 15. Example: Default & Alternative Resources res/ drawable/ icon.png background.png drawable-hdpi/ icon.png background.png • The hdpi qualifier indicates that the resources in that directory are for devices with a high-density screen. • While the images in each drawable directory are sized for a specific screen density, the filenames are the same.
  • 16. Alternative Resource Qualifier Names • Mobile country code (MCC) > mcc310, mcc310-mnc004 • Language and region > en, fr, en-rUS, fr-rFR, fr-rCA • Screen size > small, normal, large • Screen orientation > port, land • Screen density > ldpi, mdpi, hdpi, nodpi
  • 17. How Android Finds Best-matching Resource
  • 19. Resource ID • Once you provide a resource in your application (discussed in Providing Resources), you can apply it by referencing its resource ID. • All resource IDs are defined in your project's R class, which the aapt tool automatically generates. • For each type of resource, there is an R subclass (for example, R.drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R.drawable.icon). This integer is the resource ID that you can use to retrieve your resource.
  • 20. Two ways to access a resource • In code: Using an static integer from a sub-class of your R class, such as: > R.string.hello (string is the resource type and hello is the resource name) • In XML: Using a special XML syntax that also corresponds to the resource ID defined in your R class, such as: > @string/hello (string is the resource type and hello is the resource name)
  • 22. Accessing Resources In Code • You can use a resource in code by passing the resource ID as a method parameter. • For example, you can set an ImageView to use the res/drawable/myimage.png resource using setImageResource(): ImageView imageView = (ImageView) findViewById(R.id.myimageview); imageView.setImageResource(R.drawable.myimage);
  • 23. Example: Accessing Resources in Code // Load a background for the current screen from a drawable resource getWindow().setBackgroundDrawableResource(R.drawable.my_background_image) ; // Set the Activity title by getting a string from the Resources object, because // this method requires a CharSequence rather than a resource ID getWindow().setTitle(getResources().getText(R.string.main_title)); // Load a custom layout for the current screen setContentView(R.layout.main_screen); // Set a slide in animation by getting an Animation from the Resources object mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.hyperspace_in)); // Set the text on a TextView object using a resource ID TextView msgTextView = (TextView) findViewById(R.id.msg); msgTextView.setText(R.string.hello_message);
  • 24. Accessing Platform Resources • Android contains a number of standard resources, such as styles, themes, and layouts. To access these resource, qualify your resource reference with the android package name. // Android provides a layout resource you can use for list items in a ListAdapter. // In this example, simple_list_item_1 is a layout resource defined by the // platform for items in a ListView. You can use this instead of creating your // own layout for list items. setListAdapter( new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myarray));
  • 26. Accessing Resources from XML • You can define values for some XML attributes and elements using a reference to an existing resource. • You will often do this when creating layout files, to supply strings and images for your widgets. <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/submit" />
  • 27. Example: Accessing Resources from XML • If you have the following resource file that includes a color resource and a string resource: <?xml version="1.0" encoding="utf-8"?> <resources> <color name="opaque_red">#f00</color> <string name="hello">Hello!</string> </resources> • You can use these resources in the following layout file to set the text color and text string: <?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@color/opaque_red" android:text="@string/hello" />
  • 28. Example: Accessing Resources from XML • To reference a system resource, you would need to include the package name. <?xml version="1.0" encoding="utf-8"?> <EditText xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textColor="@android:color/secondary_text_dark" android:text="@string/hello" />