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 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 tag but each style will have its name that uniquely identifies the style. Android style attributes are set using tag as shown below −
xml version="1.0" encoding="utf-8"?>
The value for the can be a keyword string, a hex color, a reference to another resource type, or other value depending on the style property.
Using Styles
Once your style is defined, you can use it in your XML Layout file using styleattribute as follows −
xml version="1.0" encoding="utf-8"?> xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
To understand the concept related to Android Style, you can check Style Demo Example.
Style Inheritance
Android supports style Inheritance in very much similar way as cascading style sheet in web design. You can use this to inherit properties from an existing style and then define only the properties that you want to change or add.
To implement a custom theme create or edit MyAndroidApp/res/values/themes.xml and add the following −
... ...
In your AndroidManifest.xml apply the theme to the activities you want to style −
Your new theme will be applied to your activity, and text is now bright red.
Applying Colors to Theme Attributes
Your color resource can then be applied to some theme attributes, such as the window background and the primary text color, by adding elements to your custom theme. These attributes are defined in your styles.xml file. For example, to apply the custom color to the window background, add the following two elements to your custom theme, defined in MyAndroidApp/res/values/styles.xml file −
... ...
Using a Custom Nine-Patch With Buttons
A nine-patch drawable is a special kind of image which can be scaled in width and height while maintaining its visual integrity. Nine-patches are the most common way to specify the appearance of Android buttons, though any drawable type can be used.
A SAMPLE OF NINE-PATCH BUTTON
Steps to create Nine-Patch Buttons
Save this bitmap as /res/drawable/my_nine_patch.9.png
Define a new style
Apply the new button style to the buttonStyle attribute of your custom theme
DEFINE A NEW STYLE
... ...
APPLY THE THEME
... ...
Android Themes
Hope you understood the concept of Style, so now let's try to understand what is a Theme. A theme is nothing but an Android style applied to an entire Activity or application, rather than an individual View.
Thus, when a style is applied as a theme, every View in the Activity or application will apply each style property that it supports. For example, you can apply the same CustomFontStyle style as a theme for an Activity and then all text inside that Activity will have green monospace font.
To set a theme for all the activities of your application, open theAndroidManifest.xml file and edit the tag to include theandroid:theme attribute with the style name. For example −
android:theme="@style/CustomFontStyle">
But if you want a theme applied to just one Activity in your application, then add the android:theme attribute to the tag only. For example −
android:theme="@style/CustomFontStyle">
There are number of default themes defined by Android which you can use directly or inherit them using parent attribute as follows −
To understand the concept related to Android Theme, you can check Theme Demo Example.
Styling the colour palette
The layout design can implementable based on them based colours, for example as following design is designed based on them colour(blue)
Above layout has designed based on style.xml file,Which has placed atres/values/
Default Styles & Themes
The Android platform provides a large collection of styles and themes that you can use in your applications. You can find a reference of all available styles in the R.style class. To use the styles listed here, replace all underscores in the style name with a period. For example, you can apply the Theme_NoTitleBar theme with "@android:style/Theme.NoTitleBar". You can see the following source code for Android styles and themes −
A service is started when an application component, such as an activity, starts it by calling startService(). Once started, a service can run in the background indefinitely, even if the component that started it is destroyed.
Bound
A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC).
A service has life cycle callback methods that you can implement to monitor changes in the service's state and you can perform work at the appropriate stage. The following diagram on the left shows the life cycle when the service is created with startService() and the diagram on the right shows the life cycle when the service is created with bindService(): (image courtesy : android.com )
To create an service, you create a Java class that extends the Service base class or one of its existing subclasses. The Service base class defines various callback methods and the most important are given below. You don't need to implement all the callbacks methods. However, it's important that you understand each one and implement those that ensure your app behaves the way users expect.
Callback
Description
onStartCommand()
The system calls this method when another component, such as an activity, requests that the service be started, by calling startService(). If you implement this method, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService() methods.
onBind()
The system calls this method when another component wants to bind with the service by calling bindService(). If you implement this method, you must provide an interface that clients use to communicate with the service, by returning an IBinder object. You must always implement this method, but if you don't want to allow binding, then you should return null.
onUnbind()
The system calls this method when all clients have disconnected from a particular interface published by the service.
onRebind()
The system calls this method when new clients have connected to the service, after it had previously been notified that all had disconnected in its onUnbind(Intent).
onCreate()
The system calls this method when the service is first created using onStartCommand() or onBind(). This call is required to perform one-time set-up.
onDestroy()
The system calls this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc.
The following skeleton service demonstrates each of the life cycle methods −
/** indicates how to behave if the service is killed */ int mStartMode;
/** interface for clients that bind */ IBinder mBinder;
/** indicates whether onRebind should be used */ boolean mAllowRebind;
/** Called when the service is being created. */ @Override publicvoid onCreate(){
}
/** The service is starting, due to a call to startService() */ @Override publicint onStartCommand(Intent intent,int flags,int startId){ return mStartMode; }
/** A client is binding to the service with bindService() */ @Override publicIBinder onBind(Intent intent){ return mBinder; }
/** Called when all clients have unbound with unbindService() */ @Override publicboolean onUnbind(Intent intent){ return mAllowRebind; }
/** Called when a client is binding to the service with bindService()*/ @Override publicvoid onRebind(Intent intent){
}
/** Called when The service is no longer used and is being destroyed */ @Override publicvoid onDestroy(){
} }
Example
This example will take you through simple steps to show how to create your own Android Service. Follow the following steps to modify the Android application we created in Hello World Example chapter:
Step
Description
1
You will use Android StudioIDE to create an Android application and name it as My Application under a package com.example.My Application as explained in the Hello World Example chapter.
2
Modify main activity file MainActivity.java to add startService() andstopService() methods.
3
Create a new java file MyService.java under the package com.example.My Application. This file will have implementation of Android service related methods.
4
Define your service in AndroidManifest.xml file using tag. An application can have one or more services without any restrictions.
5
Modify the default content of res/layout/activity_main.xml file to include two buttons in linear layout.
6
No need to change any constants in res/values/strings.xml file. Android studio take care of string values
7
Run the application to launch Android emulator and verify the result of the changes done in the application.
Following is the content of the modified main activity filesrc/com.example.My Application/MainActivity.java. This file can include each of the fundamental life cycle methods. We have added startService() andstopService() methods to start and stop the service.
// Method to start the service publicvoid startService(View view){ startService(newIntent(getBaseContext(),MyService.class)); }
// Method to stop the service publicvoid stopService(View view){ stopService(newIntent(getBaseContext(),MyService.class)); } }
Following is the content of src/com.example.My Application/MyService.java. This file can have implementation of one or more methods associated with Service based on requirements. For now we are going to implement only two methods onStartCommand() and onDestroy()−
@Override publicint onStartCommand(Intent intent,int flags,int startId){ // Let it continue running until it is stopped. Toast.makeText(this,"Service Started",Toast.LENGTH_LONG).show(); return START_STICKY; }
Let's try to run our modified Hello World! application we just modified. I assume you had created your AVD while doing environment setup. To run the app from Android studio, open one of your project's activity files and click Run icon from the tool bar. Android Studio installs the app on your AVD and starts it and if everything is fine with your set-up and application, it will display following Emulator window −
Now to start your service, let's click on Start Service button, this will start the service and as per our programming in onStartCommand() method, a message Service Started will appear on the bottom of the the simulator as follows −
To stop the service, you can click the Stop Service button.