SlideShare a Scribd company logo
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);

More Related Content

Similar to Android style resource and other resources types-chapter12 (20)

ODP
Android App Development - 03 Resources
Diego Grancini
 
PDF
Android resources
ma-polimi
 
PDF
Android resource
Krazy Koder
 
PPTX
Consistent UI Across Android Devices
Irene Duke
 
PDF
Marakana Android User Interface
Marko Gargenta
 
PPTX
Android Application Fundamentals.
Skillwise Consulting
 
PPTX
Fernando F. Gallego - Efficient Android Resources 101
Fernando Gallego
 
PPT
Lecture-2.ppt
ShivamChaturvedi67
 
PDF
Android Application Development - Level 1
Isham Rashik
 
PDF
Chapter 9 - Resources System
Sittiphol Phanvilai
 
PDF
Android Lesson 2
Daniela Da Cruz
 
PPTX
UI and UX for Mobile Developers
Mohamed Nabil, MSc.
 
PDF
Xamarin Evolve 2014 - Designing Android UIs for the Ever Changing Device Land...
mstonis
 
DOCX
Android Resources.docx
KNANTHINIMCA
 
PDF
Basics and different xml files used in android
Mahmudul Hasan
 
PDF
Advanced Skinning & Styling for Android
cephus07
 
PPT
Android Developer Meetup
Medialets
 
PDF
Coding for different resolutions
Robin Srivastava
 
PDF
Advanced Android Design Implementation
Tack Mobile
 
PDF
Material design for developers
Li Jia Li
 
Android App Development - 03 Resources
Diego Grancini
 
Android resources
ma-polimi
 
Android resource
Krazy Koder
 
Consistent UI Across Android Devices
Irene Duke
 
Marakana Android User Interface
Marko Gargenta
 
Android Application Fundamentals.
Skillwise Consulting
 
Fernando F. Gallego - Efficient Android Resources 101
Fernando Gallego
 
Lecture-2.ppt
ShivamChaturvedi67
 
Android Application Development - Level 1
Isham Rashik
 
Chapter 9 - Resources System
Sittiphol Phanvilai
 
Android Lesson 2
Daniela Da Cruz
 
UI and UX for Mobile Developers
Mohamed Nabil, MSc.
 
Xamarin Evolve 2014 - Designing Android UIs for the Ever Changing Device Land...
mstonis
 
Android Resources.docx
KNANTHINIMCA
 
Basics and different xml files used in android
Mahmudul Hasan
 
Advanced Skinning & Styling for Android
cephus07
 
Android Developer Meetup
Medialets
 
Coding for different resolutions
Robin Srivastava
 
Advanced Android Design Implementation
Tack Mobile
 
Material design for developers
Li Jia Li
 

More from Dr. Ramkumar Lakshminarayanan (20)

PPT
IT security awareness
Dr. Ramkumar Lakshminarayanan
 
PPT
Basics of IT security
Dr. Ramkumar Lakshminarayanan
 
PDF
IT Security Awareness Posters
Dr. Ramkumar Lakshminarayanan
 
PPT
Normalisation revision
Dr. Ramkumar Lakshminarayanan
 
PPTX
Windows mobile programming
Dr. Ramkumar Lakshminarayanan
 
PPTX
Concurrency control
Dr. Ramkumar Lakshminarayanan
 
PPT
Web technology today
Dr. Ramkumar Lakshminarayanan
 
PDF
Phonegap for Android
Dr. Ramkumar Lakshminarayanan
 
PDF
Create and Sell Android App (in tamil)
Dr. Ramkumar Lakshminarayanan
 
PDF
Android app - Creating Live Wallpaper (tamil)
Dr. Ramkumar Lakshminarayanan
 
PDF
Android Tips (Tamil)
Dr. Ramkumar Lakshminarayanan
 
PDF
Android Animation (in tamil)
Dr. Ramkumar Lakshminarayanan
 
PDF
Creating List in Android App (in tamil)
Dr. Ramkumar Lakshminarayanan
 
PDF
Single Touch event view in Android (in tamil)
Dr. Ramkumar Lakshminarayanan
 
PDF
Android Application using seekbar (in tamil)
Dr. Ramkumar Lakshminarayanan
 
PDF
Rating Bar in Android Example
Dr. Ramkumar Lakshminarayanan
 
PDF
Creating Image Gallery - Android app (in tamil)
Dr. Ramkumar Lakshminarayanan
 
PDF
Create Android App using web view (in tamil)
Dr. Ramkumar Lakshminarayanan
 
PDF
Hardware Interface in Android (in tamil)
Dr. Ramkumar Lakshminarayanan
 
IT security awareness
Dr. Ramkumar Lakshminarayanan
 
Basics of IT security
Dr. Ramkumar Lakshminarayanan
 
IT Security Awareness Posters
Dr. Ramkumar Lakshminarayanan
 
Normalisation revision
Dr. Ramkumar Lakshminarayanan
 
Windows mobile programming
Dr. Ramkumar Lakshminarayanan
 
Concurrency control
Dr. Ramkumar Lakshminarayanan
 
Web technology today
Dr. Ramkumar Lakshminarayanan
 
Phonegap for Android
Dr. Ramkumar Lakshminarayanan
 
Create and Sell Android App (in tamil)
Dr. Ramkumar Lakshminarayanan
 
Android app - Creating Live Wallpaper (tamil)
Dr. Ramkumar Lakshminarayanan
 
Android Tips (Tamil)
Dr. Ramkumar Lakshminarayanan
 
Android Animation (in tamil)
Dr. Ramkumar Lakshminarayanan
 
Creating List in Android App (in tamil)
Dr. Ramkumar Lakshminarayanan
 
Single Touch event view in Android (in tamil)
Dr. Ramkumar Lakshminarayanan
 
Android Application using seekbar (in tamil)
Dr. Ramkumar Lakshminarayanan
 
Rating Bar in Android Example
Dr. Ramkumar Lakshminarayanan
 
Creating Image Gallery - Android app (in tamil)
Dr. Ramkumar Lakshminarayanan
 
Create Android App using web view (in tamil)
Dr. Ramkumar Lakshminarayanan
 
Hardware Interface in Android (in tamil)
Dr. Ramkumar Lakshminarayanan
 
Ad

Recently uploaded (8)

PPTX
Mobile Apps Helping Business Grow in 2025
Infylo Techsolutions
 
PPTX
The Intersection of Emoji and NFT. What can be the Consequences?
Refit Global
 
PPT
lec2 wireless transmission exlaining.ppt
212231
 
PDF
INTERLINGUAL SYNTACTIC PARSING: AN OPTIMIZED HEAD-DRIVEN PARSING FOR ENGLISH ...
kevig
 
PDF
Building Smart, Scalable Solutions with Android App Development
Brancosoft Private Limited
 
PPT
lect 1 Introduction.ppt11112222333344455
212231
 
PDF
💡 Digital Marketing Decoded: Mastering Online Growth Strategies for 2025 🚀
marketingaura24
 
PDF
Call For Papers - International Journal on Natural Language Computing (IJNLC)
kevig
 
Mobile Apps Helping Business Grow in 2025
Infylo Techsolutions
 
The Intersection of Emoji and NFT. What can be the Consequences?
Refit Global
 
lec2 wireless transmission exlaining.ppt
212231
 
INTERLINGUAL SYNTACTIC PARSING: AN OPTIMIZED HEAD-DRIVEN PARSING FOR ENGLISH ...
kevig
 
Building Smart, Scalable Solutions with Android App Development
Brancosoft Private Limited
 
lect 1 Introduction.ppt11112222333344455
212231
 
💡 Digital Marketing Decoded: Mastering Online Growth Strategies for 2025 🚀
marketingaura24
 
Call For Papers - International Journal on Natural Language Computing (IJNLC)
kevig
 
Ad

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);