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

More Related Content

PPTX
Introduction to Android and Android Studio
PPTX
ReactJS presentation.pptx
PDF
Android activities & views
PDF
Android Location and Maps
PDF
Android animation
PPTX
SQLite database in android
PPTX
Introduction to Listview in Android
Introduction to Android and Android Studio
ReactJS presentation.pptx
Android activities & views
Android Location and Maps
Android animation
SQLite database in android
Introduction to Listview in Android

What's hot (20)

PDF
Android SDK Tutorial | Edureka
PDF
Android Components
PPTX
Android Services
PPT
android activity
PDF
Android notification
PDF
Supporting multiple screens on android
PPT
SQLITE Android
PDF
Android resources
PDF
AndroidManifest
PPTX
android sqlite
PPTX
Database in Android
PPTX
Notification android
PPTX
Android share preferences
PPTX
Android - Application Framework
PPTX
Android studio ppt
PPTX
Android Layout.pptx
PPT
android menus
PPTX
Java awt (abstract window toolkit)
PDF
Android activity
PDF
UI controls in Android
Android SDK Tutorial | Edureka
Android Components
Android Services
android activity
Android notification
Supporting multiple screens on android
SQLITE Android
Android resources
AndroidManifest
android sqlite
Database in Android
Notification android
Android share preferences
Android - Application Framework
Android studio ppt
Android Layout.pptx
android menus
Java awt (abstract window toolkit)
Android activity
UI controls in Android
Ad

Viewers also liked (12)

PDF
Android Resource Manager
PPTX
Droidcon it 2014 best practices to develop for different android device class...
PPTX
Fernando F. Gallego - Efficient Android Resources 101
DOCX
Android Basic Tutorial
PPT
Android application structure
PPTX
Android ppt
PDF
Android graphics
PDF
Android complete basic Guide
PDF
Layouts in android
PPT
Introduction to Android Fragments
KEY
Android Development: The Basics
PPTX
Android ppt
Android Resource Manager
Droidcon it 2014 best practices to develop for different android device class...
Fernando F. Gallego - Efficient Android Resources 101
Android Basic Tutorial
Android application structure
Android ppt
Android graphics
Android complete basic Guide
Layouts in android
Introduction to Android Fragments
Android Development: The Basics
Android ppt
Ad

Similar to Android resource (20)

DOCX
Android Resources.docx
ODP
Android App Development - 03 Resources
PPT
eyeryeryeryeryeyeyeryeryyerye13872085.ppt
PDF
Chapter 9 - Resources System
PDF
Android App Development 08 : Support Multiple Devices
PPTX
03 android application structure
DOCX
Android resources in android-chapter9
PDF
Androidify workshop
KEY
Android momobxl
PPT
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
DOC
Android application resources
PDF
Android Introduction
PPTX
Android application development fundamentals
PDF
Android dev tips
PDF
Android Workshop - Session 2
PDF
Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013
PPTX
Android - Day 1
PPT
viWave Study Group - Introduction to Google Android Development - Chapter 23 ...
PDF
Android Application Development - Level 1
PDF
Top Tips for Android UIs - Getting the Magic on Tablets
Android Resources.docx
Android App Development - 03 Resources
eyeryeryeryeryeyeyeryeryyerye13872085.ppt
Chapter 9 - Resources System
Android App Development 08 : Support Multiple Devices
03 android application structure
Android resources in android-chapter9
Androidify workshop
Android momobxl
Android Training Ahmedabad , Android Course Ahmedabad, Android architecture
Android application resources
Android Introduction
Android application development fundamentals
Android dev tips
Android Workshop - Session 2
Matteo Gazzurelli - Andorid introduction - Google Dev Fest 2013
Android - Day 1
viWave Study Group - Introduction to Google Android Development - Chapter 23 ...
Android Application Development - Level 1
Top Tips for Android UIs - Getting the Magic on Tablets

More from Krazy Koder (20)

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

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
Classroom Observation Tools for Teachers
PPTX
master seminar digital applications in india
PDF
Complications of Minimal Access Surgery at WLH
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
RMMM.pdf make it easy to upload and study
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Module 4: Burden of Disease Tutorial Slides S2 2025
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
Week 4 Term 3 Study Techniques revisited.pptx
Classroom Observation Tools for Teachers
master seminar digital applications in india
Complications of Minimal Access Surgery at WLH
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Basic Mud Logging Guide for educational purpose
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Types and Its function , kingdom of life
Microbial diseases, their pathogenesis and prophylaxis
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
O5-L3 Freight Transport Ops (International) V1.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Renaissance Architecture: A Journey from Faith to Humanism

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