0% found this document useful (0 votes)
21 views11 pages

Slide 5

The document discusses styles and themes in Android. It defines that a style resource defines the format and look for a UI and can be applied to individual views or entire activities. Styles are defined in XML files under res/values with a <style> tag and <item> tags to set attributes. An example style is provided that sets font properties. Event listeners are interfaces that contain callback methods that are called when a view is triggered by user interaction. The document also discusses density-independent pixels which are used to maintain consistent physical sizes of views across different screen densities by converting dp values to pixels for each device.

Uploaded by

Iftakhar Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

Slide 5

The document discusses styles and themes in Android. It defines that a style resource defines the format and look for a UI and can be applied to individual views or entire activities. Styles are defined in XML files under res/values with a <style> tag and <item> tags to set attributes. An example style is provided that sets font properties. Event listeners are interfaces that contain callback methods that are called when a view is triggered by user interaction. The document also discusses density-independent pixels which are used to maintain consistent physical sizes of views across different screen densities by converting dp values to pixels for each device.

Uploaded by

Iftakhar Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Styles and Themes

Styles

 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).
 Defining Styles
 A style is defined in an XML resource that is separate from the XML that specifies the layout. This
XML file resides under res/values/ directory of your project and will have <resources> as the root
node which is mandatory for the style file. The name of the XML file is arbitrary, but it must use the
.xml extension.
 You can define multiple styles per file using <style> tag but each style will have its name that
uniquely identifies the style. Android style attributes are set using <item> tag as shown below
Styles-Example Using Styles

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<resources>
xmlns:android="http://schemas.android.com/apk/res
<style name="CustomFontStyle"> /android"
<item name="android:layout_width">fill_parent</item> android:layout_width="fill_parent"
<item name="android:layout_height">wrap_content</item>
android:layout_height="fill_parent"
<item name="android:capitalize">characters</item>
<item name="android:typeface">monospace</item> android:orientation="vertical" >
<item name="android:textSize">12pt</item>
<item name="android:textColor">#00FF00</item>/> <TextView
</style>
</resources>
android:id="@+id/text_id"
style="@style/CustomFontStyle"
android:text="@string/hello_world" />

</LinearLayout>

The value for the <item> can be a keyword string, a hex color, a reference to another resource type, or
other value depending on the style property.
Action Listeners
Event Listeners

 Event Listeners − An event listener is an interface in the View class


that contains a single callback method. These methods will be
called by the Android framework when the View to which the
listener has been registered is triggered by user interaction with the
item in the UI
Density-Independent Pixel
 The screen of an Android device is made of rows and columns of glowing dots called pixels.
Devices can range in screen density, which means how many pixels per inch (or dots per inch)
are on the screen. For example, an mdpi (or medium density device) has 160 dots per inch, while
an xxhdpi (extra extra high density device) has 480 dots per inch.
 If we specify the size of views in pixel values, then views would appear very small on the higher
density devices, where there are many pixels packed into a small area. If a button is too small,
then it would be hard for the user to touch it.
 To achieve a consistent physical size of Views, across devices of different screen densities, we use
a unit of measure called a density-independent pixel (dp or dip, pronounced “dee
pee” or “dip”). 1 dp is equal to 1 pixel on an mdpi device. 1 dp is equal to 3 pixels on an xxhdpi
device, and so on for other devices. Per Material design guidelines, any touch target on the
screen should be at least 48dp wide by 48dp tall. That way, a button in an app on one device
will be approximately the same physical size as that button in the same app running on a device
with a different screen density.
 Android devices will automatically handle the conversion from dp to pixel values, so developers
just have to use dp values when specifying measurements in their layouts. For example, dp’s can
be used to specify the width and height of a View, shown in the illustration.

You might also like