Android Resource
•Style
•String Resource
•Using Dimen
•Using Drawable – Bitmap Images
•Using Drawable – Shape XML Images & Layer List Images
Reference link:
[Link]
[Link]
Style
• A style is a collection of attributes that specifies the appearance for a single
View. A style can specify attributes such as font color, font size, background
color, and much more.
• Styles is declared in a style resource file in res/values/, usually named [Link].
• A style specifies attributes for a particular type of view. For example, one style
might specify a button's attributes. Every attribute you specify in a style is an
attribute you can set in the layout file. Extracting all the attributes to a style
makes it easy to use and maintain them across multiple widgets.
Style
To create a new style, open your project's res/values/[Link] file. For each
style you want to create, follow these steps:
[Link] a <style> element with a name that uniquely identifies the style.
[Link] an <item> element for each style attribute you want to define.
The name in each item specifies an attribute you otherwise use as an XML
attribute in your layout. The value in the <item> element is the value for that
attribute.
You can apply the style to a view as follows:
Attribute Name
• This is the name of the Style, as you can see in the example above this name is GreenText, you
must name any Style you create so you can use it later.
Item tags
• Each such item tag defines a certain type of style, where the name of that style type is described
in the name attribute of this tag, as you saw in the example, the names are like “android:
layout_width”, “android:layout_height”, “android:textColor”, “android:typeface” and many more
names... You can see that the values for this name attribute are all properties of widgets or
containing layouts. widget that you have become familiar with in designing interfaces before. So
Style is also where you take the available properties, then define a certain value for them, and
then reapply them to widgets, which is the concept of Style.
Attribute parent
• This attribute is not required. But if you want your Style to be inherited from an existing Style
(created by you before, or from a Style of a certain library, or Style of the system), this inheritance
helps you to take advantage of the existing styles. define from the original Style, and create new
definitions that complement the missing original Style.
• Example
Attribute parent
• Note that by inheriting from your Style in the same file, you may not need to declare parent
anymore and can use dots like this.
String Resource
• A string resource provides text strings for your application with optional text
styling and formatting. There are three types of resources that can provide
your application with strings:
• String
XML resource that provides a single string.
• String Array
XML resource that provides an array of strings.
• Quantity Strings (Plurals)
XML resource that carries different strings for pluralization.
Plain String
• Plain String is roughly understood as "a string without format or style". It is simply the most basic
form of string, the text in a plain string is simply normal characters, or the character "\n" to
separate to a new line.
• Each plain string is declared in a string (1) tag, and is given a name following the name attribute
(2). Finally, the content of that plain string (3).
Plain String
• Resource reference:
In Java: [Link].string_name
In XML:@string/string_name
Style String
• The first thing with plain strings is that you cannot declare these three special characters. For
example, declaring a string as follows will result in an error from the system. Or if there is no error
message like in the case of using double quotes in the third line, it will not be possible to display
this quote on the screen.
• So when you encounter the above three special characters, with a very simple solution, you put
an extra backslash (\) in front of each of these special characters.
• A small note, is that with strings containing only one type of character ('), you have another way
to use double quotes to cover this string, then you don't need to use the character (\) anymore. .
Style String
• Displaying Characters (&), (<), (>), (…)
In addition to the three special marks mentioned above, Android's XML string also has special
characters that you cannot use directly, you must use code to display them.
So in short, you have to replace them with the following codes (no spaces after the & characters).
– & replaced by &
– < replace with & lt;
– > replace with >
– … replace with & #8230;
Style String
CDATA
You still declare it as a normal plain string, the only difference is that in the content of the
string, you must wrap the string with a CDATA declaration (1), inside CDATA are the HTML formats
you know (2 ).
Style String
Accessing String Using CDATA
You cannot access this string through XML. Which must be called through the function
[Link](getString([Link].example_for_cdata));.
Style String
SpannableString
Another perfect but also very powerful HTML alternative for String formatting is
SpannableString.
Using character “/n” separate paragraphs that need formatting.
Style String
SpannableString
String(1) will be red.
String (2) will have a font twice as large as the
surrounding strings.
String (3) when clicked will display a Toast message.
String (4) when clicked will lead to the homepage
of Yellow Code Books.
Style String
Formatting String
Syntax: %[thứ_tự_xuất_hiện$]kiểu_định_dạng
s – Used to replace with a string
d – Used to replace with an integer
f – Used to replace with a real number
Style String
Formatting String
Style String
String According to Quantity
Using Plurals: Android provides Plurals resources for handling strings based on quantity.
You can define Plurals resources in the res/values/[Link] file. For example:
Then, in your code, you can use getQuantityString to retrieve the Plurals string:
Style String
String Array
file location:
res/values/[Link]
The filename is arbitrary. The <string-array> element's name is used as the resource ID.
compiled resource datatype:
Resource pointer to an array of String s.
resource reference:
In Java: [Link].string_array_name
In XML: @[package:]array/string_array_name
Style String
String Array
syntax:
This application code retrieves a string array:
Using Dimen
Dimen Resource
+ Supported Units in Android
+ Using px or dp?
Using Dimen
Dimen Declaration
Supported Units in Android
+ dp – Density-independent Pixel
+ sp – Scale-independent Pixel
+ pt – Point
+ px – Pixel
+ mm – Milimet
+ in – Inch
Using Dimen
Using in Java
Using in kotlin
Using Dimen
Using px or dp?
In Android, you have the option to use either pixels (px) or density-independent pixels (dp,
also known as dip) when specifying dimensions for user interface elements. Each has its own
purpose and is used in different contexts.
Pixels (px):
• Pixels are the smallest units of a display screen.
• Using pixels for dimensions provides exact control over the size of UI elements.
• The drawback of using pixels is that they do not scale with different screen densities, which can result in inconsistent
layouts on devices with varying pixel densities.
Using Dimen
Density-independent pixels (dp or dip):
• Density-independent pixels are a unit of measurement that is scaled based on the screen's density.
• dp is recommended for specifying dimensions in Android because it ensures that UI elements maintain a consistent physical size
across different devices, regardless of their screen densities.
• Android will automatically scale dp values to match the device's screen density, making your UI elements look similar on various
devices.
Using dp is generally recommended for most UI elements in Android development because it helps maintain a consistent user
experience across different devices with varying screen sizes and densities. However, there may be specific situations where using pixels
is necessary, such as when working with images with fixed dimensions.
Using Drawable - Bitmap Images
Image Resources and Widgets for Displaying Images:
ImageView, ImageButton or the rest of the widgets inside Android or the layouts are also
capable of displaying images. These are just places to help you display photos on the screen. As for
how the images are organized and how they are called, that is the responsibility of the resource
+ Bitmap images
+ Image Asset Tool
+ Android Asset Studio Tool
Using Drawable - Bitmap Images
Bitmap images
Bitmaps, you probably already know, are image formats organized in a matrix of pixels,
like PNG, JPEG, JPG, TIFF, PSD, etc. images that you know.
There are many types of bitmap drawable, but Android only supports us to use the
following three formats, which are PNG, JPG and GIF. PNG is recommended, JPG is acceptable,
and GIF should be limited.
There are two types of subdirectories of res/ that you can use
to organize these bitmaps, namely drawable-xxx/ and mipmap-xxx/.
These are the folders I highlighted as below.
Using Drawable - Bitmap Images
Bitmap images
Java
Kotlin
Using Drawable - Bitmap Images
Image Asset Tool
To start Image Asset Studio, follow these steps:
1. In the Project window, select the Android view.
2. Right-click the res folder and select New > Image Asset
Using Drawable - Bitmap Images
Android Asset Studio Tool
If you don't like the available Image Asset tool above, you can find a much more interesting
online tool, Android Asset Studio.
Although this tool is quite good, it also has some hated shortcomings, such as its icons are
not grouped by topic, you will spend more time each time finding an available Clipart icon. Or after
choosing the icon you like, you have to download the zip package. Although this zip package has
icons available in each folder, it also takes you a lot of time to copy/paste them into the project, not
automatically. works like Image Asset above.
Using Drawable - Shape XML Images &
Layer List Images
Shape XML Images
Shape XML, as its name suggests, allows you to design any shape based on XML. Then
when the application is executed, the system will draw the image for you based on the XML design.
How to Draw Shape XML Images
The formula, or full syntax,
to draw an XML Shape is as follows.
Syntax : android:background="@drawable/[ten file]"
Using Drawable - Shape XML Images &
Layer List Images
Layer List Images
This section is considered an extension of Shape XML above. If Shape XML only helps
you draw simple shapes, then Layer List helps combine many of those simple shapes together to
form a more complex shape.
How to Draw layer List Images