2. This Leson
● Styling your app
● App Permission Basics
● Creating & Registering a BroadcastReceiver.
3. Styling Your App
● Similiar to styling webpages in a separate CSS file,
Android Apps can be given their own style in a separate
XML file.
● This is another new XML file within resources and
resides in the res/values directory. Can be called
anything (e.g. Often style.xml)
● Typical properties which are styled are Font, Colour,
Padding, Height, Border and many more.
4. Styling Parameters
● We can put the styles of GUI components (e.g. TextView,
Button etc) with them in their respective GUI XML files.
However it is cleaner and easier to manage style related
parameters in a separate XML file.
● We can define as many styles as we wish in our styles XML
file using separately named <style></style> tags within the
overall <resource></resource> tags.
● Between each set of <style></style> tags we put one or more
<item></item> tags for each property that style wishes to use.
5. A Basic Style XML File
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="style1">
<item name="android:textColor">#FF0000</item>
<item name="android:textSize">30sp</item>
</style>
<style name="style2">
<item
name="android:background">#C0C0C0</item>
</style>
sp = Scale
Independent
Pixel. Should
be used for
sizing fonts.
dp for
everything
else.
6. Using a <style>
● Every <style> has a name. We use this to refer to it in
our GUI XML to style a component (or components)
● If we had a TextView in an Activity or Fragment which
we wished to style using style1 from previous slide.
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/welcome_message”
style=”@style/style1”
/>
layout_width
and
layout_height
often placed
in style XML
file.
7. Themes
A Theme is just a <style> but applied to a whole Activity
or Application. Done in AndroidManifest.xml.
To apply a Theme to an <activity>
<activity android:theme="@android:style/style1">
To apply a Theme to an <application>
<application android:theme="@style/style2">
8. Style Inheritance
● Somewhat similar to Java class inheritance, one <style>
can extend another. Android platform provides simple
notation to achieve this.
● Inheritance avoids code repetition. Let’s build on top of
textstyle (already defined) and define a new style called
specialtextstyle.
<style name="textstyle.specialtextstyle ">
<item name="android:typeface">monospace</item>
</style>
specialtextstyle
extends from
textstyle and adds
attribute
android:typeface
9. Tailoring System Themes
● Android comes with a number of pre-existing themes
built-in to the Android platform.
● Often it is easier to just tailor these by changing just
the attribute you need to to suit you apps styling needs.
To extend a system theme you use the parent attribute.
<resources>
<style name="mySpecialTheme" parent="android:Theme.Material">
<item name="android:colorPrimary">put your own value here</item>
<item name="android:colorPrimaryDark">put your own value here</item>
</style>
</resources>
10. A Useful Diagram
This shows some commonly
styled areas in an app which
are useful to know.
11. Common Style Attributes
● FAQ : How do I know what attributes I can style ?
● There are a huge number of attributes available to
style in Android (Alot which you’ll probably never use)
● Navigation of this whole area is still not great but start
here in styles.xml (Contains all system default styling)
https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml
● Search by component and see what can be styled on it.
(e.g. What can be styled on a Button ?)
12. Style Example
<style name="Widget.Button">
<item name="background">@drawable/btn_default</item>
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="textAppearance">?attr/textAppearanceSmallInverse</item>
<item name="textColor">@color/primary_text_light</item>
<item name="gravity">center_vertical|center_horizontal</item>
</style>● What we see here is the default styling for a Button. You
can see highlighted in yellow what can be styled.
13. Android Security
● We’ve seen how each app has its own area to run in
with it’s own internal storage, so it cannot adversley
affect other apps, data or the user’s experience.
● If we write an app which requires resources or data
elsewhere on the device it requests permission.
● Permissions to other data (e.g. Location, Phonebook etc)
must be granted (or rejected) by the user when the
app is being installed. Not possible at runtime.
14. Asking for Permission
● An app requests a specific permission via the
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.app.myapp" >
<uses-permission android:name="android.permission.INTERNET"/>
...
</manifest>
● The extensive list of system permissions that are
available to request are here
http://developer.android.com/reference/android/Manifest.permission.html
15. BroadcastReceiever
Note : These are LOW LEVEL
events to do with the actual
hardware. We’ll see later that we
can listen to software events on
the phone as well. e.g. Phone call
received, SMS received etc
● A BroadcastReceiver is a component which can register
with the Android OS to “listen” to certain system or
application events (NB : These events are Broadcast as Intents)
● Typical SYSTEM BROADCAST would be the following :
o Device Boot Completed
o Power Connected
o Power Disconnected
o Battery Low
o Battery OK
(Constants in Intent class)
16. Creating a BroadcastReceiever
● Setting up a BroadcastReceiver involves two steps
o Writing the new BroadcastReceiver class
(by extending BroadcastReceiver)
o Registering the new BroadcastReceiver FOR SPECIFIC Intents
(in AndroidManifest.xml)
public class MyBatteryReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
//Do something here
}
}
Events are
represented
as Intents.
17. Registering a
BroadcastReceiver
● When registering a BroadcastReceiver in the
AndroidManifest.xml we need to define what it is
interested in hearing!
● Within the manifest we must remember two important
things :
o 1. Register for the event (Intent) we’re interested in.
o 2. Have PERMISSION to listen for it.
● NOTE : Most broadcast Intents require that permission
to be granted to an app wishing to hear them.
18. The Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
<uses-permission android:name="android.permission.BATTERY_STATS" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
</activity>
<receiver android:name=".MyBatteryReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_LOW"
/>
</intent-filter>
</receiver>
</application>
</manifest>
Remember : To
mark itself as
being able to
respond to a
particular
Intent, we must
use <intent-
filter>
19. TelephonyService
● We’ve already seen how to listen to basic system
events to do with the hardware on the phone. Can we
listen for when the phone rings via a BroadcastReceiver
? YES!
● Another constantly running System Service (e.g.
NotifcationService from last week) on your phone is the
TelephonyService.
● TelephonyService has public static final String
constants for possible phone states (EXTRA_STATE_IDLE,
EXTRA_STATE_RINGING, EXTRA_STATE_OFFHOOK)
20. Responding to Phone State
Changes
● We first need permission to listen to changes in the
state of the phone by adding the following line to
AndroidManifest.xml
<uses-permission
android:name="android.permission.READ_PHONE_STATE"/>
● Next we need to write our own class (we’ll call it
MyPhoneReceiver) which extends BroadcastReceiver.
● REMEMBER : We receive notifications into our
BroadcastReceiver subclass as an Intent.
(An Intent has a Bundle attached to it called “extras”)
21. BrodcastReceiver Code
public class MyPhoneReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = extras
.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
}
}
}
}
extras Bundle
carries
information
about the
phone call
22. Declare <receiver>
● Remember to declare your BroadcastReceiver in the
AndroidManifest.xml
<receiver android:name="MyPhoneReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"
</action>
</intent-filter>
</receiver>