SlideShare ist ein Scribd-Unternehmen logo
1 von 7
Unit 12
Style Resource and Other Resources Types
By
Dr. Ramkumar Lakshminarayanan
Introduction
In this unit we will discuss about the Style Resource and other resources like Bool, Color,
Dimension, ID, Integer, Integer Array, Typed Array.
Style Resource
A style resource defines the format and look for a UI. A style can be applied to an
individual View (from within a layout file) or to an entire Activity or application (from within
the manifest file).
A style is a simple resource that is referenced using the value provided in the name
attribute (not the name of the XML file). As such, you can combine style resources with other
simple resources in the one XML file, under one <resources> element.
file location:
res/values/filename.xml
The filename is arbitrary. The element's name will be used as the resource ID.
resource reference:
In XML: @[package:]style/style_name
Example for Style Resource
XML file for the style (saved in res/values/):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomText" parent="@style/Text">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
XML file that applies the style to a TextView (saved in res/layout/):
Additional Resource Types
 Bool
 Color
 Dimension
 ID
 Integer
 Integer Array
 Typed Array
Bool XML resource that carries a boolean value. Color XML resource that carries a color
value (a hexadecimal color). Dimension XML resource that carries a dimension value (with a
unit of measure). ID XML resource that provides a unique identifier for application resources
and components. Integer XML resource that carries an integer value. Integer Array XML
resource that provides an array of integers. Typed Array XML resource that provides a
TypedArray which you can use for an array of drawables.
Example for Bool Resource Type
XML file saved at res/values-small/bools.xml:
This application code retrieves the boolean:
<?xml version="1.0" encoding="utf-8"?>
<EditText
style="@style/CustomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="screen_small">true</bool>
<bool name="adjust_view_bounds">true</bool>
</resources>
This layout XML uses the boolean for an attribute:
Color Resource Type
A color value defined in XML. The color is specified with an RGB value and alpha
channel. You can use a color resource any place that accepts a hexadecimal color value. You can
also use a color resource when a drawable resource is expected in XML (for example,
android:drawable="@color/green").
The value always begins with a pound (#) character and then followed by the Alpha-Red-
Green-Blue information in one of the following formats:
 #RGB
 #ARGB
 #RRGGBB
 #AARRGGBB
Example for Color Resource Type
XML file saved at res/values/colors.xml:
This application code retrieves the color resource:
Resources res = getResources();
boolean screenIsSmall = res.getBoolean(R.bool.screen_small);
<ImageView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="@drawable/logo"
android:adjustViewBounds="@bool/adjust_view_bounds" />
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="opaque_red">#f00</color>
<color name="translucent_red">#80ff0000</color>
</resources>
Resources res = getResources();
int color = res.getColor(R.color.opaque_red);
This layout XML applies the color to an attribute:
Dimension Resource Type
A dimension value defined in XML. A dimension is specified with a number followed by
a unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by
Android:
Density-independent Pixels – dp
An abstract unit that is based on the physical density of the screen. These units are
relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running
on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor
appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels
used for 1dp is scaled down.
Scale-independent Pixels – sp
This is like the dp unit, but it is also scaled by the user's font size preference. It is
recommend you use this unit when specifying font sizes, so they will be adjusted for both the
screen density and the user's preference.
Point - pt
Points - 1/72 of an inch based on the physical size of the screen.
Pixels – px
Corresponds to actual pixels on the screen. This unit of measure is not recommended
because the actual representation can vary across devices; each devices may have a different
number of pixels per inch and may have more or fewer total pixels available on the screen.
Millimeters – mm
Based on the physical size of the screen.
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/translucent_red"
android:text="Hello"/>
Inches – in
Based on the physical size of the screen.
Example for Dimension Resource Type:
XML file saved at res/values/dimens.xml:
This application code retrieves a dimension:
This layout XML applies dimensions to attributes:
ID
A unique resource ID defined in XML. Using the name you provide in the <item>
element, the Android developer tools create a unique integer in your project's R.java class, which
you can use as an identifier for an application resources.
Example for ID Resource Type
XML file saved at res/values/ids.xml:
Then, this layout snippet uses the "button_ok" ID for a Button widget:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="ball_radius">30dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);
<TextView
android:layout_height="@dimen/textview_height"
android:layout_width="@dimen/textview_width"
android:textSize="@dimen/font_size"/>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="button_ok" />
<item type="id" name="dialog_exit" />
</resources>
<Button android:id="@id/button_ok"
style="@style/button_style" />
Integer
An integer is a simple resource that is referenced using the value provided in the name
attribute (not the name of the XML file). As such, you can combine integer resources with other
simple resources in the one XML file, under one <resources> element.
Integer Array
An integer array is a simple resource that is referenced using the value provided in the
name attribute (not the name of the XML file). As such, you can combine integer array resources
with other simple resources in the one XML file, under one <resources> element.
Example for Integer Array
XML file saved at res/values/integers.xml:
This application code retrieves the integer array:
Typed Array Resource Type
A typed array is a simple resource that is referenced using the value provided in the name
attribute (not the name of the XML file). As such, you can combine typed array resources with
other simple resources in the one XML file, under one <resources> element.
Example for Typed Array
XML file saved at res/values/arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="bits">
<item>4</item>
<item>8</item>
<item>16</item>
<item>32</item>
</integer-array>
</resources>
Resources res = getResources();
int[] bits = res.getIntArray(R.array.bits);
This application code retrieves each array and then obtains the first entry in each array:
Summary
In this unit we discussed about the style resource and other resource type you can
externalize.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="icons">
<item>@drawable/home</item>
<item>@drawable/settings</item>
<item>@drawable/logout</item>
</array>
<array name="colors">
<item>#FFFF0000</item>
<item>#FF00FF00</item>
<item>#FF0000FF</item>
</array>
</resources>
Resources res = getResources();
TypedArray icons =
res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);
TypedArray colors =
res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);

Weitere ähnliche Inhalte

Ähnlich wie Android style resource and other resources types-chapter12

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
 
Basics and different xml files used in android
Basics and different xml files used in androidBasics and different xml files used in android
Basics and different xml files used in androidMahmudul Hasan
 
03 android application structure
03 android application structure03 android application structure
03 android application structureSokngim Sa
 
mobile application development -unit-3-
mobile application development  -unit-3-mobile application development  -unit-3-
mobile application development -unit-3-TejamFandat
 
Android Resources.docx
Android Resources.docxAndroid Resources.docx
Android Resources.docxKNANTHINIMCA
 
Android App Development - 03 Resources
Android App Development - 03 ResourcesAndroid App Development - 03 Resources
Android App Development - 03 ResourcesDiego Grancini
 
Android training (android style)
Android training (android style)Android training (android style)
Android training (android style)Khaled Anaqwa
 
Building a simple user interface lesson2
Building a simple user interface lesson2Building a simple user interface lesson2
Building a simple user interface lesson2Kalluri Vinay Reddy
 
Android Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectAndroid Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectJoemarie Amparo
 
Android Development for Beginners with Sample Project - Day 1
Android Development for Beginners with Sample Project - Day 1Android Development for Beginners with Sample Project - Day 1
Android Development for Beginners with Sample Project - Day 1Joemarie Amparo
 
Synapseindia android apps introduction hello world
Synapseindia android apps introduction hello worldSynapseindia android apps introduction hello world
Synapseindia android apps introduction hello worldTarunsingh198
 
Android resource
Android resourceAndroid resource
Android resourceKrazy Koder
 
Android ui layout
Android ui layoutAndroid ui layout
Android ui layoutKrazy Koder
 

Ähnlich wie Android style resource and other resources types-chapter12 (20)

Android resources in android-chapter9
Android resources in android-chapter9Android resources in android-chapter9
Android resources in android-chapter9
 
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
 
Basics and different xml files used in android
Basics and different xml files used in androidBasics and different xml files used in android
Basics and different xml files used in android
 
03 android application structure
03 android application structure03 android application structure
03 android application structure
 
Android Application Fundamentals.
Android Application Fundamentals.Android Application Fundamentals.
Android Application Fundamentals.
 
Ap quiz app
Ap quiz appAp quiz app
Ap quiz app
 
mobile application development -unit-3-
mobile application development  -unit-3-mobile application development  -unit-3-
mobile application development -unit-3-
 
Android Resources.docx
Android Resources.docxAndroid Resources.docx
Android Resources.docx
 
Android App Development - 03 Resources
Android App Development - 03 ResourcesAndroid App Development - 03 Resources
Android App Development - 03 Resources
 
Android training (android style)
Android training (android style)Android training (android style)
Android training (android style)
 
Building a simple user interface lesson2
Building a simple user interface lesson2Building a simple user interface lesson2
Building a simple user interface lesson2
 
Android Development Made Easy - With Sample Project
Android Development Made Easy - With Sample ProjectAndroid Development Made Easy - With Sample Project
Android Development Made Easy - With Sample Project
 
Android Development for Beginners with Sample Project - Day 1
Android Development for Beginners with Sample Project - Day 1Android Development for Beginners with Sample Project - Day 1
Android Development for Beginners with Sample Project - Day 1
 
Synapseindia android apps introduction hello world
Synapseindia android apps introduction hello worldSynapseindia android apps introduction hello world
Synapseindia android apps introduction hello world
 
Lecture-2.ppt
Lecture-2.pptLecture-2.ppt
Lecture-2.ppt
 
Android resource
Android resourceAndroid resource
Android resource
 
Chapter 9 - Resources System
Chapter 9 - Resources SystemChapter 9 - Resources System
Chapter 9 - Resources System
 
Android Programming.pptx
Android Programming.pptxAndroid Programming.pptx
Android Programming.pptx
 
Lesson 10
Lesson 10Lesson 10
Lesson 10
 
Android ui layout
Android ui layoutAndroid ui layout
Android ui layout
 

Mehr von Dr. Ramkumar Lakshminarayanan

Mehr von Dr. Ramkumar Lakshminarayanan (20)

IT security awareness
IT security awarenessIT security awareness
IT security awareness
 
Basics of IT security
Basics of IT securityBasics of IT security
Basics of IT security
 
IT Security Awareness Posters
IT Security Awareness PostersIT Security Awareness Posters
IT Security Awareness Posters
 
Normalisation revision
Normalisation revisionNormalisation revision
Normalisation revision
 
Windows mobile programming
Windows mobile programmingWindows mobile programming
Windows mobile programming
 
Concurrency control
Concurrency controlConcurrency control
Concurrency control
 
Web technology today
Web technology todayWeb technology today
Web technology today
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Phonegap for Android
Phonegap for AndroidPhonegap for Android
Phonegap for Android
 
Create and Sell Android App (in tamil)
Create and Sell Android App (in tamil)Create and Sell Android App (in tamil)
Create and Sell Android App (in tamil)
 
Android app - Creating Live Wallpaper (tamil)
Android app - Creating Live Wallpaper (tamil)Android app - Creating Live Wallpaper (tamil)
Android app - Creating Live Wallpaper (tamil)
 
Android Tips (Tamil)
Android Tips (Tamil)Android Tips (Tamil)
Android Tips (Tamil)
 
Android Animation (in tamil)
Android Animation (in tamil)Android Animation (in tamil)
Android Animation (in tamil)
 
Creating List in Android App (in tamil)
Creating List in Android App (in tamil)Creating List in Android App (in tamil)
Creating List in Android App (in tamil)
 
Single Touch event view in Android (in tamil)
Single Touch event view in Android (in tamil)Single Touch event view in Android (in tamil)
Single Touch event view in Android (in tamil)
 
Android Application using seekbar (in tamil)
Android Application using seekbar (in tamil)Android Application using seekbar (in tamil)
Android Application using seekbar (in tamil)
 
Rating Bar in Android Example
Rating Bar in Android ExampleRating Bar in Android Example
Rating Bar in Android Example
 
Creating Image Gallery - Android app (in tamil)
Creating Image Gallery - Android app (in tamil)Creating Image Gallery - Android app (in tamil)
Creating Image Gallery - Android app (in tamil)
 
Create Android App using web view (in tamil)
Create Android App using web view (in tamil)Create Android App using web view (in tamil)
Create Android App using web view (in tamil)
 
Hardware Interface in Android (in tamil)
Hardware Interface in Android (in tamil)Hardware Interface in Android (in tamil)
Hardware Interface in Android (in tamil)
 

Kürzlich hochgeladen

BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceDelhi Call girls
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPsychicRuben LoveSpells
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceanilsa9823
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Pooja Nehwal
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceanilsa9823
 
9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7Pooja Nehwal
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRnishacall1
 

Kürzlich hochgeladen (7)

BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
 
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual serviceCALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
CALL ON ➥8923113531 🔝Call Girls Saharaganj Lucknow best sexual service
 
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
Call US Pooja 9892124323 ✓Call Girls In Mira Road ( Mumbai ) secure service,
 
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun serviceCALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
CALL ON ➥8923113531 🔝Call Girls Gomti Nagar Lucknow best Night Fun service
 
9892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x79892124323 | Book Call Girls in Juhu and escort services 24x7
9892124323 | Book Call Girls in Juhu and escort services 24x7
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
 

Android style resource and other resources types-chapter12

  • 1. Unit 12 Style Resource and Other Resources Types By Dr. Ramkumar Lakshminarayanan Introduction In this unit we will discuss about the Style Resource and other resources like Bool, Color, Dimension, ID, Integer, Integer Array, Typed Array. Style Resource A style resource defines the format and look for a UI. A style can be applied to an individual View (from within a layout file) or to an entire Activity or application (from within the manifest file). A style is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine style resources with other simple resources in the one XML file, under one <resources> element. file location: res/values/filename.xml The filename is arbitrary. The element's name will be used as the resource ID. resource reference: In XML: @[package:]style/style_name Example for Style Resource XML file for the style (saved in res/values/): <?xml version="1.0" encoding="utf-8"?> <resources> <style name="CustomText" parent="@style/Text"> <item name="android:textSize">20sp</item> <item name="android:textColor">#008</item> </style> </resources>
  • 2. XML file that applies the style to a TextView (saved in res/layout/): Additional Resource Types  Bool  Color  Dimension  ID  Integer  Integer Array  Typed Array Bool XML resource that carries a boolean value. Color XML resource that carries a color value (a hexadecimal color). Dimension XML resource that carries a dimension value (with a unit of measure). ID XML resource that provides a unique identifier for application resources and components. Integer XML resource that carries an integer value. Integer Array XML resource that provides an array of integers. Typed Array XML resource that provides a TypedArray which you can use for an array of drawables. Example for Bool Resource Type XML file saved at res/values-small/bools.xml: This application code retrieves the boolean: <?xml version="1.0" encoding="utf-8"?> <EditText style="@style/CustomText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Hello, World!" /> <?xml version="1.0" encoding="utf-8"?> <resources> <bool name="screen_small">true</bool> <bool name="adjust_view_bounds">true</bool> </resources>
  • 3. This layout XML uses the boolean for an attribute: Color Resource Type A color value defined in XML. The color is specified with an RGB value and alpha channel. You can use a color resource any place that accepts a hexadecimal color value. You can also use a color resource when a drawable resource is expected in XML (for example, android:drawable="@color/green"). The value always begins with a pound (#) character and then followed by the Alpha-Red- Green-Blue information in one of the following formats:  #RGB  #ARGB  #RRGGBB  #AARRGGBB Example for Color Resource Type XML file saved at res/values/colors.xml: This application code retrieves the color resource: Resources res = getResources(); boolean screenIsSmall = res.getBoolean(R.bool.screen_small); <ImageView android:layout_height="fill_parent" android:layout_width="fill_parent" android:src="@drawable/logo" android:adjustViewBounds="@bool/adjust_view_bounds" /> <?xml version="1.0" encoding="utf-8"?> <resources> <color name="opaque_red">#f00</color> <color name="translucent_red">#80ff0000</color> </resources> Resources res = getResources(); int color = res.getColor(R.color.opaque_red);
  • 4. This layout XML applies the color to an attribute: Dimension Resource Type A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by Android: Density-independent Pixels – dp An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. Scale-independent Pixels – sp This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference. Point - pt Points - 1/72 of an inch based on the physical size of the screen. Pixels – px Corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen. Millimeters – mm Based on the physical size of the screen. <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@color/translucent_red" android:text="Hello"/>
  • 5. Inches – in Based on the physical size of the screen. Example for Dimension Resource Type: XML file saved at res/values/dimens.xml: This application code retrieves a dimension: This layout XML applies dimensions to attributes: ID A unique resource ID defined in XML. Using the name you provide in the <item> element, the Android developer tools create a unique integer in your project's R.java class, which you can use as an identifier for an application resources. Example for ID Resource Type XML file saved at res/values/ids.xml: Then, this layout snippet uses the "button_ok" ID for a Button widget: <?xml version="1.0" encoding="utf-8"?> <resources> <dimen name="textview_height">25dp</dimen> <dimen name="textview_width">150dp</dimen> <dimen name="ball_radius">30dp</dimen> <dimen name="font_size">16sp</dimen> </resources> Resources res = getResources(); float fontSize = res.getDimension(R.dimen.font_size); <TextView android:layout_height="@dimen/textview_height" android:layout_width="@dimen/textview_width" android:textSize="@dimen/font_size"/> <?xml version="1.0" encoding="utf-8"?> <resources> <item type="id" name="button_ok" /> <item type="id" name="dialog_exit" /> </resources> <Button android:id="@id/button_ok" style="@style/button_style" />
  • 6. Integer An integer is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine integer resources with other simple resources in the one XML file, under one <resources> element. Integer Array An integer array is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine integer array resources with other simple resources in the one XML file, under one <resources> element. Example for Integer Array XML file saved at res/values/integers.xml: This application code retrieves the integer array: Typed Array Resource Type A typed array is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine typed array resources with other simple resources in the one XML file, under one <resources> element. Example for Typed Array XML file saved at res/values/arrays.xml: <?xml version="1.0" encoding="utf-8"?> <resources> <integer-array name="bits"> <item>4</item> <item>8</item> <item>16</item> <item>32</item> </integer-array> </resources> Resources res = getResources(); int[] bits = res.getIntArray(R.array.bits);
  • 7. This application code retrieves each array and then obtains the first entry in each array: Summary In this unit we discussed about the style resource and other resource type you can externalize. <?xml version="1.0" encoding="utf-8"?> <resources> <array name="icons"> <item>@drawable/home</item> <item>@drawable/settings</item> <item>@drawable/logout</item> </array> <array name="colors"> <item>#FFFF0000</item> <item>#FF00FF00</item> <item>#FF0000FF</item> </array> </resources> Resources res = getResources(); TypedArray icons = res.obtainTypedArray(R.array.icons); Drawable drawable = icons.getDrawable(0); TypedArray colors = res.obtainTypedArray(R.array.colors); int color = colors.getColor(0,0);