Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 659395 of Getting Started (JavaScript Tutorial)

  • Revision slug: Web/JavaScript/Getting_Started
  • Revision title: Getting Started (JavaScript Tutorial)
  • Revision id: 659395
  • Created:
  • Creator: dhimangatum
  • Is current revision? No
  • Comment

Revision Content

Why You Need to Read Our Android Tutorial?

Javtpoint.com develops Android Tutorial which allows you to solve each and every question. Al We made it in easy manner with full of examples so that you do not need to go anywhere. We ensure you that if you will use our Android tutorial then you will get an appropriate answer easily.

What is Android?

Android is an open source and Linux-based Operating System for cell phones, for example, cell phones and tablet machines. Android was produced by the Open Handset Alliance, headed by Google, and different organizations.

Android offers a bound together approach to application improvement for cell phones which implies designers require create for Android, and their applications ought to have the capacity to run on distinctive gadgets fueled by Android.

Features of Android

  • Beautiful UI: Android OS basic screen provides a beautiful and intuitive user interface.
  • Connectivity: GSM/EDGE, IDEN, CDMA, EV-DO, UMTS, Bluetooth, Wi-Fi, LTE, NFC and WiMAX.
  • Storage: SQLite, a lightweight relational database, is used for data storage purposes.
  • Media support :H.263, H.264, MPEG-4 SP, AMR, AMR-WB, AAC, HE-AAC, AAC 5.1, MP3, MIDI, Ogg Vorbis, WAV, JPEG, PNG, GIF, and BMP
  • Messaging: SMS and MMS
  • Web browser: Based on the open-source WebKit layout engine, coupled with Chrome's V8 JavaScript engine supporting HTML5 and CSS3.
  • Multi-touch: Android has native support for multi-touch which was initially made available in handsets such as the HTC Hero.
  • Multi-tasking: User can jump from one task to another and same time various application can run simultaneously.
  • Resizable widgets: Widgets are resizable, so users can expand them to show more content or shrink them to save space
  • Multi-Language:Supports single direction and bi-directional text.
  • GCM:Google Cloud Messaging (GCM) is a service that lets developers send short message data to their users on Android devices, without needing a proprietary sync solution.
  • Wi-Fi Direct: A technology that lets apps discover and pair directly, over a high-bandwidth peer-to-peer connection.
  • Android Beam: A popular NFC-based technology that lets users instantly share, just by touching two NFC-enabled phones together.

Step 1 - Setup Java Development Kit (JDK)

You can download the latest version of Java JDK from Oracle's Java site: Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.

set PATH=C:\jdk1.6.0_15\bin;%PATH%

set JAVA_HOME=C:\jdk1.6.0_15

On Linux, if the SDK is installed in /usr/local/jdk1.6.0_15 and you use the C shell, you would put the following code into your .cshrc file.

setenv PATH /usr/local/jdk1.6.0_15/bin:$PATH
setenv JAVA_HOME /usr/local/jdk1.6.0_15

Step 2 - Setup Android SDK

You can download the latest version of Android SDK from Android official website: Android SDK Downloads. If you are installing SDK on Windows machine, then you will find a installer_rXX-windows.exe, so just download and run this exe which will launch Android SDK Tool Setup wizard to guide you through out of the installation, so just follow the instructions carefully. Finally you will haveAndroid SDK Tools installed on your machine.

If you are installing SDK either on Mac OS or Linux, check the instructions provided along with the downloaded android-sdk_rXX-macosx.zip file for Mac OS and android-sdk_rXX-linux.tgz file for Linux. This tutorial will consider that you are going to setup your environment on Windows machine having Windows 7 operating system.

So let's launch Android SDK Manager using the option All Programs > Android SDK Tools > SDK Manager, this will give you following window:

Once you launched SDK manager, its time to install other required packages. By default it will list down total 7 packages to be installed, but I will suggest to de-select Documentation for Android SDK andSamples for SDK packages to reduce installation time. Next click Install 7 Packages button to proceed, which will display following dialogue box:

If you agree to install all the packages, select Accept All radio button and proceed by clicking Install button. Now let SDK manager do its work and you go, pick up a cup of coffee and wait until all the packages are installed. It may take some time depending on your internet connection. Once all the packages are installed, you can close SDK manager using top-right cross button.

Step 3 - Setup Eclipse IDE

To install Eclipse IDE, download the latest Eclipse binaries from https://www.eclipse.org/downloads/. Once you downloaded the installation, unpack the binary distribution into a convenient location. For example in C:\eclipse on windows, or /usr/local/eclipse on Linux and finally set PATH variable appropriately. Eclipse can be started by executing the following commands on windows machine, or you can simply double click on eclipse.exe

 %C:\eclipse\eclipse.exe

Eclipse can be started by executing the following commands on Linux machine:

$/usr/local/eclipse/eclipse

After a successful startup, if everything is fine then it should display following result:

 

Step 4 - Setup Android Development Tools (ADT) Plug-in

This step will help you in setting Android Development Tool plugin for Eclipse. Let's start with launching Eclipse and then, choose Help > Software Updates > Install New Software. This will display the following dialogue box

Now use Add button to add ADT Plugin as name and https://dl-ssl.google.com/android/eclipse/ as the location. Then click OK to add this location, as soon as you will click OK button to add this location, Eclipse starts searching for the plug-in available the given location and finally lists down the found plugins.

Now select all the listed plug-ins using Select All button and click Next button which will guide you ahead to install Android Development Tools and other required plugins.

 

Step 5 - Create Android Virtual Device

To test your Android applications you will need a virtual Android device. So before we start writing our code, let us create an Android virtual device. Launch Android AVD Manager using Eclipse menu optionsWindow > AVD Manager> which will launch Android AVD Manager. Use New button to create a new Android Virtual Device and enter the following information, before clicking Create AVD button.

If your AVD is created successfully it means your environment is ready for Android application development. If you like, you can close this window using top-right cross button. Better you re-start your machine and once you are done with this last step, you are ready to proceed for your first Android example but before that we will see few more important concepts related to Android Application Development.

Android Architecture

 

Linux kernel

At the bottom of the layers is Linux - Linux 2.6 with approximately 115 patches. This provides basic system functionality like process management, memory management, device management like camera, keypad, display etc. Also, the kernel handles all the things that Linux is really good at such as networking and a vast array of device drivers, which take the pain out of interfacing to peripheral hardware.

Libraries

On top of Linux kernel there is a set of libraries including open-source Web browser engine WebKit, well known library libc, SQLite database which is a useful repository for storage and sharing of application data, libraries to play and record audio and video, SSL libraries responsible for Internet security etc.

Android Runtime

This is the third section of the architecture and available on the second layer from the bottom. This section provides a key component called Dalvik Virtual Machine which is a kind of Java Virtual Machine specially designed and optimized for Android.

The Dalvik VM makes use of Linux core features like memory management and multi-threading, which is intrinsic in the Java language. The Dalvik VM enables every Android application to run in its own process, with its own instance of the Dalvik virtual machine.

The Android runtime also provides a set of core libraries which enable Android application developers to write Android applications using standard Java programming language.

Application Framework

The Application Framework layer provides many higher-level services to applications in the form of Java classes. Application developers are allowed to make use of these services in their applications.

Applications

Application components are the essential building blocks of an Android application. These components are loosely coupled by the application manifest file AndroidManifest.xml that describes each component of the application and how they interact.

Components

Description

Activities

They dictate the UI and handle the user interaction to the smartphone screen

Services

They handle background processing associated with an application.

Broadcast Receivers

They handle communication between Android OS and applications.

Content Providers

They handle data and database management issues.

 

Activities

An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched.

An activity is implemented as a subclass of Activity class as follows:

public class MainActivity extends Activity {}

Services

A service is a component that runs in the background to perform long-running operations. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity.

A service is implemented as a subclass of Service class as follows:

public class MyService extends Service {}

Broadcast Receivers

Broadcast Receivers simply respond to broadcast messages from other applications or from the system. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and each message is broadcasted as an Intent object.

public class MyReceiver  extends  BroadcastReceiver {}

A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. The data may be stored in the file system, the database or somewhere else entirely.

A content provider is implemented as a subclass of ContentProvider class and must implement a standard set of APIs that enable other applications to perform transactions.

public class MyContentProvider extends  ContentProvider {}

 

Additional Components

 

Components

Description

Fragments

Represents a behavior or a portion of user interface in an Activity.

Views

UI elements that are drawn onscreen including buttons, lists forms etc.

Layouts

View hierarchies that control screen format and appearance of the views.

Intents

Messages wiring components together.

Resources

External elements, such as strings, constants and drawables pictures.

Manifest

Configurations file for the application.

Android Layout Types

 

S.N.

Layout & Description

1

Linear Layout
LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally.

2

Relative Layout
RelativeLayout is a view group that displays child views in relative positions.

3

Table Layout
TableLayout is a view that groups views into rows and columns.

4

Absolute Layout
AbsoluteLayout enables you to specify the exact location of its children.

5

Frame Layout
The FrameLayout is a placeholder on screen that you can use to display a single view.

6

List View
ListView is a view group that displays a list of scrollable items.

7

Grid View
GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.

Layout Attributes

 

Attribute

Description

android:id

This is the ID which uniquely identifies the view.

android:layout_width

This is the width of the layout.

android:layout_height

This is the height of the layout

android:layout_marginTop

This is the extra space on the top side of the layout.

android:layout_marginBottom

This is the extra space on the bottom side of the layout.

android:layout_marginLeft

This is the extra space on the left side of the layout.

android:layout_marginRight

This is the extra space on the right side of the layout.

android:layout_gravity

This specifies how child Views are positioned.

android:layout_weight

This specifies how much of the extra space in the layout should be allocated to the View.

android:layout_x

This specifies the x-coordinate of the layout.

android:layout_y

This specifies the y-coordinate of the layout.

android:layout_width

This is the width of the layout.

android:layout_width

This is the width of the layout.

android:paddingLeft

This is the left padding filled for the layout.

android:paddingRight

This is the right padding filled for the layout.

android:paddingTop

This is the top padding filled for the layout.

android:paddingBottom

This is the bottom padding filled for the layout.

Here width and height are the dimension of the layout/view which can be specified in terms of dp (Density-independent Pixels), sp ( Scale-independent Pixels), pt ( Points which is 1/72 of an inch), px( Pixels), mm ( Millimeters) and finally in (inches).

You can specify width and height with exact measurements but more often, you will use one of these constants to set the width or height:

·         android:layout_width=wrap_content tells your view to size itself to the dimensions required by its content.

·         android:layout_width=fill_parent tells your view to become as big as its parent view.

Gravity attribute plays important role in positioning the view object and it can take one or more (separated by '|') of the following constant values.

View Identification

A view object may have a unique ID assigned to it which will identify the View uniquely within the tree. The syntax for an ID, inside an XML tag is:

android:id="@+id/my_button"

Following is a brief description of @ and + signs:

·         The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource.

·         The plus-symbol (+) means that this is a new resource name that must be created and added to our resources. To create an instance of the view object and capture it from the layout, use the following:

Button myButton = (Button) findViewById(R.id.my_button);

Android UI Controls

S.N.

UI Control & Description

1

Text View
This control is used to display text to the user.

2

Edit Text
EditText is a predefined subclass of TextView that includes rich editing capabilities.

3

Auto Complete Text View
The AutoCompleteTextView is a view that is similar to EditText, except that it shows a list of completion suggestions automatically while the user is typing.

4

Button
A push-button that can be pressed, or clicked, by the user to perform an action.

5

Image Button
AbsoluteLayout enables you to specify the exact location of its children.

6

Checkbox
An on/off switch that can be toggled by the user. You should use checkboxes when presenting users with a group of selectable options that are not mutually exclusive.

7

Toggle Button
An on/off button with a light indicator.

8

Radio Button
The RadioButton has two states: either checked or unchecked.

9

Radio Group
A RadioGroup is used to group together one or more RadioButtons.

10

Progress Bar
The ProgressBar view provides visual feedback about some ongoing tasks, such as when you are performing a task in the background.

11

Spinner
A drop-down list that allows users to select one value from a set.

12

Time Picker
The TimePicker view enables users to select a time of the day, in either 24-hour mode or AM/PM mode.

13

Date Picker
The DatePicker view enables users to select a date of the day.

Create UI Controls

As explained in previous chapter, a view object may have a unique ID assigned to it which will identify the View uniquely within the tree. The syntax for an ID, inside an XML tag is:

android:id="@+id/text_id"

To create a UI Control/View/Widget you will have to define a view/widget in the layout file and assign it a unique ID as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent"
   android:orientation="vertical" >
   
   <TextView android:id="@+id/text_id"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="I am a TextView" />
</LinearLayout>

Then finally create an instance of the Control object and capture it from the layout, use the following:

TextView myText = (TextView) findViewById(R.id.text_id);

 

Android Event Handling

 Event Listeners & Event Handlers

Event Handler

Event Listener & Description

onClick()

OnClickListener()
This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. You will use onClick() event handler to handle such event.

onLongClick()

OnLongClickListener()
This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. for one or more seconds. You will use onLongClick() event handler to handle such event.

onFocusChange()

OnFocusChangeListener()
This is called when the widget looses its focus ie. user goes away from the view item. You will use onFocusChange() event handler to handle such event.

onKey()

OnFocusChangeListener()
This is called when the user is focused on the item and presses or releases a hardware key on the device. You will use onKey() event handler to handle such event.

onTouch()

OnTouchListener()
This is called when the user presses the key, releases the key, or any movement gesture on the screen. You will use onTouch() event handler to handle such event.

onMenuItemClick()

OnMenuItemClickListener()
This is called when the user selects a menu item. You will use onMenuItemClick() event handler to handle such event.

 Event Listeners Registration:

Event Registration is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event. Though there are several tricky ways to register your event listener for any event, but I'm going to list down only top 3 ways, out of which you can use any of them based on the situation.

· Using an Anonymous Inner Class

· Activity class implements the Listener interface.

· Using Layout file activity_main.xml to specify event handler directly.

Below section will provide you detailed examples on all the three scenarios

 

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.

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="CustomFontStyle">
      <item name="android:layout_width">fill_parent</item>
      <item name="android:layout_height">wrap_content</item>
      <item name="android:capitalize">characters</item>
      <item name="android:typeface">monospace</item>
      <item name="android:textSize">12pt</item>
      <item name="android:textColor">#00FF00</item>/> 
   </style>
</resources>

 

Using Styles

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
    <TextView android:id="@+id/text_id"
   style="@style/CustomFontStyle"
   android:text="@string/hello_world" />
 </LinearLayout>

 

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.

Its simple, to create a new style LargeFont that inherits the CustomFontStyle style defined above, but make the font size big, you can author the new style like this:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="CustomFontStyle.LargeFont">
      <item name="android:textSize">20ps</item>
   </style>
</resources>

 

You can reference this new style as @style/CustomFontStyle.LargeFont in your XML Layout file. You can continue inheriting like this as many times as you'd like, by chaining names with periods. For example, you can extend FontStyle.LargeFont to be Red, with:

 

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="CustomFontStyle.LargeFont.Red">
      <item name="android:textColor">#FF0000</item>/> 
   </style>
</resources>

This technique for inheritance by chaining together names only works for styles defined by your own resources. You can't inherit Android built-in styles this way. To reference an Android built-in style, such as TextAppearance, you must use the parent attribute as shown below:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <style name="CustomFontStyle" parent="@android:style/TextAppearance">
      <item name="android:layout_width">fill_parent</item>
      <item name="android:layout_height">wrap_content</item>
      <item name="android:capitalize">characters</item>
      <item name="android:typeface">monospace</item>
      <item name="android:textSize">12pt</item>
      <item name="android:textColor">#00FF00</item>/> 
   </style>
</resources>

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 the AndroidManifest.xml file and edit the<application> tag to include the android:theme attribute with the style name. For example

<application 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 <activity> tag only. For example:

<activity 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:

<style name="CustomTheme" parent="android:Theme.Light">
    ...
</style>

 

Creating a Simple Custom Component

 

The simplest way to create your custom component is to extend an existing widget class or subclass with your own class if you want to extend the functionality of existing widget like Button, TextView, EditText, ListView, CheckBox etc. otherwise you can do everything yourself by starting with theandroid.view.View class.

At its simplest form you will have to write your constructors corresponding to all the constructors of the base class. For example if you are going to extend TextView to create a DateView then following three constructors will be created for DateView class:

public class DateView extends TextView {
   public DateView(Context context) {
      super(context);
      //--- Additional custom code --
   }
    public DateView(Context context, AttributeSet attrs) {
      super(context, attrs);
      //--- Additional custom code --
   }
    public DateView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      //--- Additional custom code --
   }
}

Because you have created DateView as child of TextView so it will have access on all the attributes, methods and events related to TextView and you will be able to use them without any further implementation. You will implement additional custom functionality inside your own code as explained in the given examples below.

If you have requirement for implementing custom drawing/sizing for your custom widgets then you need to override onMeasure(int widthMeasureSpec, int heightMeasureSpec) and onDraw(Canvas canvas) methods. If you are not going to resize or change the shape of your built-in component then you do not need either of these methods in your custom component.

The onMeasure() method coordinate with the layout manager to report the widget's width and height, and you need to call setMeasuredDimension(int width, int height) from inside this method to report the dimensions.

You can then execute your custom drawing inside the onDraw(Canvas canvas) method, where android.graphis.Canvas is pretty similar to its counterpart in Swing, and has methods such as drawRect(), drawLine(), drawString(), drawBitmap() etc. which you can use to draw your component.

INSTANTIATE USING CODE INSIDE ACTIVITY CLASS

@Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
          DateView dateView = new DateView(this);
     setContentView(dateView);
 }

 

INSTANTIATE USING LAYOUT XML FILE

 

Traditionally you use Layout XML file to instantiate your built-in widgets, same concept will apply on your custom widgets as well so you will be able to instantiate your custom component using Layout XML file as explained below. Here com.example.dateviewdemo is the package where you have put all the code related to DateView class and DateView is Java class name where you have put complete logic of your custom component.

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    
    <com.example.dateviewdemo.DateView
     android:layout_width="match_parent"
     android:layout_height="wrap_content" 
     android:textColor="#fff"
     android:textSize="40sp"
     android:background="#000"
     />
</RelativeLayout>

 

Custom Component with Custom Attributes

 

We have seen how we can extend functionality of built-in widgets but in both the examples given above we saw that extended component can make use of all the default attributes of its parent class. But consider a situation when you want to create your own attribute from scratch. Below is a simple procedure to create and use new attributes for Android Custom components. Consider we want to introduce three attributes and will use them as shown below:

<com.example.dateviewdemo.DateView
   android:layout_width="match_parent"
   android:layout_height="wrap_content" 
   android:textColor="#fff"
   android:textSize="40sp"
   custom:delimiter="-"
   custom:fancyText="true"
/>

 

STEP 1

 

The first step to enable us to use our custom attributes is to define them in a new xml file underres/values/ and call it attrs.xml. Let's have a look on an example attrs.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="DateView">
   <attr name="delimiter" format="string"/>
   <attr name="fancyText" format="boolean"/>
   </declare-styleable>
</resources>

 

STEP 2

                                                                                                                                         

Your second step will be to read these attributes from Layout XML file and set them for the component. This logic will go in the constructors that get passed an AttributeSet, since that is what contains the XML attributes. To read the values in the XML, you need to first create a TypedArray from theAttributeSet, then use that to read and set the values as shown in the below example code:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DateView);
 
final int N = a.getIndexCount();
for (int i = 0; i < N; ++i)
{
   int attr = a.getIndex(i);
   switch (attr)
   {
      case R.styleable.DateView_delimiter:
         String delimiter = a.getString(attr);
         //...do something with delimiter...
         break;
      case R.styleable.DateView_fancyText:
         boolean fancyText = a.getBoolean(attr, false);
         //...do something with fancyText...
         break;
   }
}
a.recycle();

STEP 3

 

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    xmlns:custom="https://schemas.android.com/apk/res/com.example.dateviewdemo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <com.example.dateviewdemo.DateView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:textColor="#fff"
    android:textSize="40sp"
    custom:delimiter="-"
    custom:fancyText="true"
    />
 </RelativeLayout>

 

The important part isxmlns:custom="https://schemas.android.com/apk/res/com.example.dateviewdemo". Note thathttps://schemas.android.com/apk/res/ will remain as is, but last part will be set to your package name and also that you can use anything after xmlns:, in this example I used custom, but you could use any name you like.

<o:p></o:p>

Revision Source

<h2>Why You Need to Read Our Android Tutorial?</h2>
<p><span style="color:#000000;">Javtpoint.com develops </span><a href="https://www.javatpoint.com/android-tutorial"><span style="color:#000000;">Android Tutorial</span></a><span style="color:#000000;"> which allows you to solve each and every question. Al We made it in easy manner with full of examples so that you do not need to go anywhere. We ensure you that if you will use our Android tutorial then you will get an appropriate answer easily.</span></p>
<h2><span style="line-height: 1.5;">What is Android?</span></h2>
<p><span style="color:#000000;"><span style="line-height: 1.5;">Android is an open source and Linux-based Operating System for cell phones, for example, cell phones and tablet machines. Android was produced by the Open Handset Alliance, headed by Google, and different organizations.</span></span></p>
<p><span style="color:#000000;">Android offers a bound together approach to application improvement for cell phones which implies designers require create for Android, and their applications ought to have the capacity to run on distinctive gadgets fueled by Android.</span></p>
<h2>Features of Android</h2>
<ul>
 <li><span style="color:#000000;"><strong>Beautiful UI:&nbsp;Android OS basic screen provides a beautiful and intuitive user interface.</strong></span></li>
 <li><span style="color:#000000;"><strong>Connectivity:&nbsp;GSM/EDGE,&nbsp;IDEN,&nbsp;CDMA, EV-DO, UMTS, Bluetooth, Wi-Fi, LTE, NFC and WiMAX.</strong></span></li>
 <li><span style="color:#000000;"><strong>Storage:&nbsp;SQLite, a lightweight relational database, is used for data storage purposes.</strong></span></li>
 <li><span style="color:#000000;"><strong>Media support :H.263, H.264, MPEG-4 SP, AMR, AMR-WB, AAC, HE-AAC, AAC 5.1, MP3, MIDI, Ogg Vorbis, WAV, JPEG, PNG, GIF, and BMP</strong></span></li>
 <li><span style="color:#000000;"><strong>Messaging:&nbsp;SMS&nbsp;and MMS</strong></span></li>
 <li><span style="color:#000000;"><strong>Web browser:&nbsp;Based on the open-source WebKit layout engine, coupled with Chrome's V8 JavaScript engine supporting HTML5 and CSS3.</strong></span></li>
 <li><span style="color:#000000;"><strong>Multi-touch:&nbsp;Android has native support for multi-touch which was initially made available in handsets such as the HTC Hero.</strong></span></li>
 <li><span style="color:#000000;"><strong>Multi-tasking:&nbsp;User can jump from one task to another and same time various application can run simultaneously.</strong></span></li>
 <li><span style="color:#000000;"><strong><strong>Resizable&nbsp;widgets:&nbsp;Widgets are resizable, so users can expand them to show more content or shrink them to save space</strong></strong></span></li>
 <li><span style="color:#000000;">Multi-Language:Supports single direction and bi-directional text.</span></li>
 <li><span style="color:#000000;"><strong>GCM:Google Cloud Messaging (GCM) is a service that lets developers send short message data to their users on Android devices, without&nbsp;needing a proprietary sync solution.</strong></span></li>
 <li><span style="color:#000000;"><strong><strong>Wi-Fi&nbsp;Direct:&nbsp;A technology that lets apps discover and pair directly, over a high-bandwidth peer-to-peer connection.</strong></strong></span></li>
 <li><span style="color:#000000;"><strong><strong><strong>Android Beam:&nbsp;A popular NFC-based technology that lets users instantly share, just by touching two NFC-enabled phones together.</strong></strong></strong></span></li>
</ul>
<p><span style="font-size: 2.142857142857143rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">Step 1 - Setup Java Development Kit (JDK)</span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">You can download the latest version of Java JDK from Oracle's Java site:&nbsp;Java SE Downloads. You will find instructions for installing JDK in downloaded files, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains&nbsp;</span><strong style="line-height: 1.5;">java</strong><span style="line-height: 1.5;">&nbsp;and&nbsp;</span><strong style="line-height: 1.5;">javac</strong><span style="line-height: 1.5;">, typically java_install_dir/bin and java_install_dir respectively.</span></span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">set PATH=C:\jdk1.6.0_15\bin;%PATH%</span></span></p>
<div>
 <p><span style="color:#000000;">set JAVA_HOME=C:\jdk1.6.0_15</span></p>
</div>
<p><span style="color:#000000;"><span style="line-height: 1.5;">On Linux, if the SDK is installed in /usr/local/jdk1.6.0_15 and you use the C shell, you would put the following code into your&nbsp;</span><strong style="line-height: 1.5;">.cshrc</strong><span style="line-height: 1.5;">&nbsp;file.</span></span></p>
<div>
 <pre>
<span style="color:#000000;">setenv PATH /usr/local/jdk1.6.0_15/bin:$PATH</span></pre>
 <pre>
<span style="color:#000000;">setenv JAVA_HOME /usr/local/jdk1.6.0_15</span></pre>
</div>
<h2>Step 2 - Setup Android SDK</h2>
<p><span style="color:#000000;">You can download the latest version of Android SDK from Android official website:&nbsp;</span><a href="https://developer.android.com/sdk/index.html" target="_blank"><span style="color:#000000;">Android SDK Downloads</span></a><span style="color:#000000;">. If you are installing SDK on Windows machine, then you will find a&nbsp;<em>installer_rXX-windows.exe</em>, so just download and run this exe which will launch&nbsp;<em>Android SDK Tool Setup</em>&nbsp;wizard to guide you through out of the installation, so just follow the instructions carefully. Finally you will have<em>Android SDK Tools</em>&nbsp;installed on your machine.</span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">If you are installing SDK either on Mac OS or Linux, check the instructions provided along with the downloaded&nbsp;</span><em style="line-height: 1.5;">android-sdk_rXX-macosx.zip</em><span style="line-height: 1.5;">&nbsp;file for Mac OS and&nbsp;</span><em style="line-height: 1.5;">android-sdk_rXX-linux.tgz</em><span style="line-height: 1.5;">&nbsp;file for Linux. This tutorial will consider that you are going to setup your environment on Windows machine having Windows 7 operating system.</span></span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">So let's launch&nbsp;</span><em style="line-height: 1.5;">Android SDK Manager</em><span style="line-height: 1.5;">&nbsp;using the option&nbsp;</span><strong style="line-height: 1.5;">All Programs &gt; Android SDK Tools &gt; SDK Manager</strong><span style="line-height: 1.5;">, this will give you following window:</span></span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">Once you launched SDK manager, its time to install other required packages. By default it will list down total 7 packages to be installed, but I will suggest to de-select&nbsp;</span><em style="line-height: 1.5;">Documentation for Android SDK</em><span style="line-height: 1.5;">&nbsp;and</span><em style="line-height: 1.5;">Samples for SDK</em><span style="line-height: 1.5;">&nbsp;packages to reduce installation time. Next click&nbsp;</span><strong style="line-height: 1.5;">Install 7 Packages</strong><span style="line-height: 1.5;">&nbsp;button to proceed, which will display following dialogue box:</span></span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">If you agree to install all the packages, select&nbsp;</span><strong style="line-height: 1.5;">Accept All</strong><span style="line-height: 1.5;">&nbsp;radio button and proceed by clicking&nbsp;</span><strong style="line-height: 1.5;">Install </strong><span style="line-height: 1.5;">button. Now let SDK manager do its work and you go, pick up a cup of coffee and wait until all the packages are installed. It may take some time depending on your internet connection. Once all the packages are installed, you can close SDK manager using top-right cross button.</span></span></p>
<p><span style="font-size: 2.142857142857143rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">Step 3 - Setup Eclipse IDE</span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">To install Eclipse IDE, download the latest Eclipse binaries from&nbsp;</span></span><a href="https://www.eclipse.org/downloads/" style="line-height: 1.5;" target="_blank"><span style="color:#000000;">https://www.eclipse.org/downloads/</span></a><span style="color:#000000;"><span style="line-height: 1.5;">. Once you downloaded the installation, unpack the binary distribution into a convenient location. For example in C:\eclipse on windows, or /usr/local/eclipse on Linux and finally set PATH variable appropriately. Eclipse can be started by executing the following commands on windows machine, or you can simply double click on eclipse.exe</span></span></p>
<div>
 <pre>
<span style="color:#000000;">&nbsp;%C:\eclipse\eclipse.exe</span></pre>
</div>
<p><span style="color:#000000;">Eclipse can be started by executing the following commands on Linux machine:</span></p>
<div>
 <pre>
<span style="color:#000000;">$/usr/local/eclipse/eclipse</span></pre>
</div>
<p><span style="color:#000000;">After a successful startup, if everything is fine then it should display following result:</span></p>
<p>&nbsp;</p>
<h2>Step 4 - Setup Android Development Tools (ADT) Plug-in</h2>
<p><span style="font-size: 12pt; line-height: 115%; font-family: Georgia, serif; color: black; background-image: initial; background-attachment: initial; background-size: initial; background-origin: initial; background-clip: initial; background-position: initial; background-repeat: initial;">This step will help you in setting Android Development Tool plugin for Eclipse. Let's start with launching Eclipse and then, choose<span class="apple-converted-space">&nbsp;</span>Help &gt; Software Updates &gt; Install New Software. This will display the following dialogue box</span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">Now use&nbsp;</span><strong style="line-height: 1.5;">Add</strong><span style="line-height: 1.5;">&nbsp;button to add&nbsp;</span><em style="line-height: 1.5;">ADT Plugin</em><span style="line-height: 1.5;">&nbsp;as name and&nbsp;</span><em style="line-height: 1.5;">https://dl-ssl.google.com/android/eclipse/</em><span style="line-height: 1.5;">&nbsp;as the location. Then click OK to add this location, as soon as you will click OK button to add this location, Eclipse starts searching for the plug-in available the given location and finally lists down the found plugins.</span></span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">Now select all the listed plug-ins using&nbsp;</span><strong style="line-height: 1.5;">Select All</strong><span style="line-height: 1.5;">&nbsp;button and click&nbsp;</span><strong style="line-height: 1.5;">Next</strong><span style="line-height: 1.5;">&nbsp;button which will guide you ahead to install Android Development Tools and other required plugins.</span></span></p>
<p>&nbsp;</p>
<h2>Step 5 - Create Android Virtual Device</h2>
<p><span style="color:#000000;">To test your Android applications you will need a virtual Android device. So before we start writing our code, let us create an Android virtual device. Launch Android AVD Manager using Eclipse menu options<strong>Window &gt; AVD Manager&gt;</strong>&nbsp;which will launch Android AVD Manager. Use&nbsp;<strong>New</strong>&nbsp;button to create a new Android Virtual Device and enter the following information, before clicking&nbsp;<strong>Create AVD</strong>&nbsp;button.</span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">If your AVD is created successfully it means your environment is ready for Android application development. If you like, you can close this window using top-right cross button. Better you re-start your machine and once you are done with this last step, you are ready to proceed for your first Android example but before that we will see few more important concepts related to Android Application Development.</span></span></p>
<h2><span style="font-family: Georgia, serif; font-size: 18pt; text-align: center; line-height: 1.5;">Android Architecture</span></h2>
<p>&nbsp;</p>
<h2>Linux kernel</h2>
<p><span style="color:#000000;">At the bottom of the layers is Linux - Linux 2.6 with approximately 115 patches. This provides basic system functionality like process management, memory management, device management like camera, keypad, display etc. Also, the kernel handles all the things that Linux is really good at such as networking and a vast array of device drivers, which take the pain out of interfacing to peripheral hardware.</span></p>
<p><span style="font-size: 2.142857142857143rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">Libraries</span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">On top of Linux kernel there is a set of libraries including open-source Web browser engine WebKit, well known library libc, SQLite database which is a useful repository for storage and sharing of application data, libraries to play and record audio and video, SSL libraries responsible for Internet security etc.</span></span></p>
<p><span style="font-size: 2.142857142857143rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">Android Runtime</span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">This is the third section of the architecture and available on the second layer from the bottom. This section provides a key component called&nbsp;</span><strong style="line-height: 1.5;">Dalvik Virtual Machine</strong><span style="line-height: 1.5;">&nbsp;which is a kind of Java Virtual Machine specially designed and optimized for Android.</span></span></p>
<p><span style="color:#000000;">The Dalvik VM makes use of Linux core features like memory management and multi-threading, which is intrinsic in the Java language. The Dalvik VM enables every Android application to run in its own process, with its own instance of the Dalvik virtual machine.</span></p>
<p><span style="color:#000000;">The Android runtime also provides a set of core libraries which enable Android application developers to write Android applications using standard Java programming language.</span></p>
<p><span style="font-size: 2.142857142857143rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">Application Framework</span></p>
<p><span style="color:#000000;"><span style="line-height: 1.5;">The Application Framework layer provides many higher-level services to applications in the form of Java classes. Application developers are allowed to make use of these services in their applications.</span></span></p>
<h2>Applications</h2>
<p><span style="color:#000000;">Application components are the essential building blocks of an Android application. These components are loosely coupled by the application manifest file&nbsp;<em>AndroidManifest.xml</em>&nbsp;that describes each component of the application and how they interact.</span></p>
<table border="1" cellpadding="0" cellspacing="0" style="width:560px;" width="560">
 <tbody>
  <tr>
   <td style="width:129px;">
    <p><span style="color:#000000;"><strong>Components</strong></span></p>
   </td>
   <td>
    <p><span style="color:#000000;"><strong>Description</strong></span></p>
   </td>
  </tr>
  <tr>
   <td>
    <p><span style="color:#000000;">Activities</span></p>
   </td>
   <td>
    <p><span style="color:#000000;">They dictate the UI and handle the user interaction to the smartphone screen</span></p>
   </td>
  </tr>
  <tr>
   <td>
    <p><span style="color:#000000;">Services</span></p>
   </td>
   <td>
    <p><span style="color:#000000;">They handle background processing associated with an application.</span></p>
   </td>
  </tr>
  <tr>
   <td>
    <p><span style="color:#000000;">Broadcast Receivers</span></p>
   </td>
   <td>
    <p><span style="color:#000000;">They handle communication between Android OS and applications.</span></p>
   </td>
  </tr>
  <tr>
   <td style="height:1px;">
    <p><span style="color:#000000;">Content Providers</span></p>
   </td>
   <td style="height:1px;">
    <p><span style="color:#000000;">They handle data and database management issues.</span></p>
   </td>
  </tr>
 </tbody>
</table>
<p>&nbsp;</p>
<h2>Activities</h2>
<p><span style="color:#000000;">An activity represents a single screen with a user interface. For example, an email application might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. If an application has more than one activity, then one of them should be marked as the activity that is presented when the application is launched.</span></p>
<p><span style="color:#000000;">An activity is implemented as a subclass of&nbsp;<strong>Activity</strong>&nbsp;class as follows:</span></p>
<div>
 <pre>
<span style="color:#000000;">public class MainActivity extends Activity {}</span></pre>
</div>
<h2>Services</h2>
<p><span style="color:#000000;">A service is a component that runs in the background to perform long-running operations. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity.</span></p>
<p><span style="color:#000000;">A service is implemented as a subclass of&nbsp;<strong>Service</strong>&nbsp;class as follows:</span></p>
<div>
 <pre>
<span style="color:#000000;">public class MyService extends Service {}</span></pre>
</div>
<h2>Broadcast Receivers</h2>
<p><span style="color:#000000;">Broadcast Receivers simply respond to broadcast messages from other applications or from the system. For example, applications can also initiate broadcasts to let other applications know that some data has been downloaded to the device and is available for them to use, so this is broadcast receiver who will intercept this communication and will initiate appropriate action.</span></p>
<p><span style="color:#000000;">A broadcast receiver is implemented as a subclass of&nbsp;<strong>BroadcastReceiver</strong>&nbsp;class and each message is broadcasted as an&nbsp;<strong>Intent</strong>&nbsp;object.</span></p>
<div>
 <pre>
<span style="color:#000000;">public class MyReceiver&nbsp; extends&nbsp; BroadcastReceiver {}</span></pre>
 <p><span style="color:#000000;"><span style="line-height: 1.5;">A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the&nbsp;</span><em style="line-height: 1.5;">ContentResolver</em><span style="line-height: 1.5;">&nbsp;class. The data may be stored in the file system, the database or somewhere else entirely.</span></span></p>
 <p><span style="color:#000000;"><span style="line-height: 1.5;">A content provider is implemented as a subclass of&nbsp;</span><strong style="line-height: 1.5;">ContentProvider</strong><span style="line-height: 1.5;">&nbsp;class and must implement a standard set of APIs that enable other applications to perform transactions.</span></span></p>
 <div>
  <pre>
<span style="color:#000000;">public class MyContentProvider extends&nbsp; ContentProvider {}</span></pre>
  <p>&nbsp;</p>
  <h2>Additional Components</h2>
  <p>&nbsp;</p>
  <table border="1" cellpadding="0" cellspacing="0" style="width:560px;" width="560">
   <tbody>
    <tr>
     <td style="width:129px;">
      <p><strong>Components</strong></p>
     </td>
     <td>
      <p><strong>Description</strong></p>
     </td>
    </tr>
    <tr>
     <td>
      <p>Fragments</p>
     </td>
     <td>
      <p>Represents a behavior or a portion of user interface in an Activity.</p>
     </td>
    </tr>
    <tr>
     <td>
      <p>Views</p>
     </td>
     <td>
      <p>UI elements that are drawn onscreen including buttons, lists forms etc.</p>
     </td>
    </tr>
    <tr>
     <td>
      <p>Layouts</p>
     </td>
     <td>
      <p>View hierarchies that control screen format and appearance of the views.</p>
     </td>
    </tr>
    <tr>
     <td>
      <p>Intents</p>
     </td>
     <td>
      <p>Messages wiring components together.</p>
     </td>
    </tr>
    <tr>
     <td>
      <p>Resources</p>
     </td>
     <td>
      <p>External elements, such as strings, constants and drawables pictures.</p>
     </td>
    </tr>
    <tr>
     <td>
      <p>Manifest</p>
     </td>
     <td>
      <p>Configurations file for the application.</p>
     </td>
    </tr>
   </tbody>
  </table>
  <h2>Android Layout Types</h2>
  <p>&nbsp;</p>
  <table border="1" cellpadding="0" cellspacing="0" style="width:560px;" width="560">
   <tbody>
    <tr>
     <td style="width:5px;">
      <p><span style="color:#000000;"><strong>S.N.</strong></span></p>
     </td>
     <td>
      <p><span style="color:#000000;"><strong>Layout &amp; Description</strong></span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">1</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Linear Layout<br />
       LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">2</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Relative Layout<br />
       RelativeLayout is a view group that displays child views in relative positions.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">3</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Table Layout<br />
       TableLayout is a view that groups views into rows and columns.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">4</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Absolute Layout<br />
       AbsoluteLayout enables you to specify the exact location of its children.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">5</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Frame Layout<br />
       The FrameLayout is a placeholder on screen that you can use to display a single view.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">6</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">List View<br />
       ListView is a view group that displays a list of scrollable items.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">7</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Grid View<br />
       GridView is a ViewGroup that displays items in a two-dimensional, scrollable grid.</span></p>
     </td>
    </tr>
   </tbody>
  </table>
  <p><span style="font-size: 2.142857142857143rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">Layout Attributes</span></p>
  <p>&nbsp;</p>
  <table border="1" cellpadding="0" cellspacing="0" style="width:560px;" width="560">
   <tbody>
    <tr>
     <td style="width:162px;">
      <p><span style="color:#000000;"><strong>Attribute</strong></span></p>
     </td>
     <td>
      <p><span style="color:#000000;"><strong>Description</strong></span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:id</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the ID which uniquely identifies the view.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_width</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the width of the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_height</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the height of the layout</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_marginTop</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the extra space on the top side of the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_marginBottom</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the extra space on the bottom side of the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_marginLeft</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the extra space on the left side of the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_marginRight</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the extra space on the right side of the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_gravity</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This specifies how child Views are positioned.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_weight</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This specifies how much of the extra space in the layout should be allocated to the View.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_x</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This specifies the x-coordinate of the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_y</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This specifies the y-coordinate of the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_width</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the width of the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:layout_width</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the width of the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:paddingLeft</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the left padding filled for the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:paddingRight</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the right padding filled for the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:paddingTop</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the top padding filled for the layout.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">android:paddingBottom</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">This is the bottom padding filled for the layout.</span></p>
     </td>
    </tr>
   </tbody>
  </table>
  <p><span style="color: rgb(0, 0, 0); line-height: 1.5;">Here width and height are the dimension of the layout/view which can be specified in terms of dp (Density-independent Pixels), sp ( Scale-independent Pixels), pt ( Points which is 1/72 of an inch), px( Pixels), mm ( Millimeters) and finally in (inches).</span></p>
  <p><span style="color:#000000;">You can specify width and height with exact measurements but more often, you will use one of these constants to set the width or height:</span></p>
  <p><span style="color:#000000;">·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>android:layout_width=wrap_content</strong>&nbsp;tells your view to size itself to the dimensions required by its content.</span></p>
  <p><span style="color:#000000;">·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>android:layout_width=fill_parent</strong>&nbsp;tells your view to become as big as its parent view.</span></p>
  <p><span style="color:#000000;">Gravity attribute plays important role in positioning the view object and it can take one or more (separated by '|') of the following constant values.</span></p>
  <p><span style="font-size: 2.142857142857143rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">View Identification</span></p>
  <p><span style="color:#000000;"><span style="line-height: 1.5;">A view object may have a unique ID assigned to it which will identify the View uniquely within the tree. The syntax for an ID, inside an XML tag is:</span></span></p>
  <div>
   <pre>
<span style="color:#000000;">android:id="@+id/my_button"</span></pre>
  </div>
  <p><span style="color:#000000;">Following is a brief description of @ and + signs:</span></p>
  <p><span style="color:#000000;">·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The at-symbol (@) at the beginning of the string indicates that the XML parser should parse and expand the rest of the ID string and identify it as an ID resource.</span></p>
  <p><span style="color:#000000;">·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; The plus-symbol (+) means that this is a new resource name that must be created and added to our resources. To create an instance of the view object and capture it from the layout, use the following:</span></p>
  <div>
   <pre>
<span style="color:#000000;">Button myButton = (Button) findViewById(R.id.my_button);</span></pre>
  </div>
  <h2>Android UI Controls</h2>
  <table border="1" cellpadding="0" cellspacing="0" style="width:560px;" width="560">
   <tbody>
    <tr>
     <td style="width:5px;">
      <p><span style="color:#000000;"><strong>S.N.</strong></span></p>
     </td>
     <td>
      <p><span style="color:#000000;"><strong>UI Control &amp; Description</strong></span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">1</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Text View<br />
       This control is used to display text to the user.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">2</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Edit Text<br />
       EditText is a predefined subclass of TextView that includes rich editing capabilities.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">3</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Auto Complete Text View<br />
       The AutoCompleteTextView is a view that is similar to EditText, except that it shows a list of completion suggestions automatically while the user is typing.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">4</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Button<br />
       A push-button that can be pressed, or clicked, by the user to perform an action.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">5</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Image Button<br />
       AbsoluteLayout enables you to specify the exact location of its children.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">6</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Checkbox<br />
       An on/off switch that can be toggled by the user. You should use checkboxes when presenting users with a group of selectable options that are not mutually exclusive.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">7</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Toggle Button<br />
       An on/off button with a light indicator.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">8</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Radio Button<br />
       The RadioButton has two states: either checked or unchecked.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">9</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Radio Group<br />
       A RadioGroup is used to group together one or more RadioButtons.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">10</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Progress Bar<br />
       The ProgressBar view provides visual feedback about some ongoing tasks, such as when you are performing a task in the background.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">11</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Spinner<br />
       A drop-down list that allows users to select one value from a set.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">12</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Time Picker<br />
       The TimePicker view enables users to select a time of the day, in either 24-hour mode or AM/PM mode.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">13</span></p>
     </td>
     <td>
      <p><span style="color:#000000;">Date Picker<br />
       The DatePicker view enables users to select a date of the day.</span></p>
     </td>
    </tr>
   </tbody>
  </table>
  <h2>Create UI Controls</h2>
  <p><span style="color:#000000;">As explained in previous chapter, a view object may have a unique ID assigned to it which will identify the View uniquely within the tree. The syntax for an ID, inside an XML tag is:</span></p>
  <div>
   <pre>
<span style="color:#000000;">android:id="@+id/text_id"</span></pre>
  </div>
  <p><span style="color:#000000;">To create a UI Control/View/Widget you will have to define a view/widget in the layout file and assign it a unique ID as follows:</span></p>
  <div>
   <pre>
<span style="color:#000000;">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span></pre>
   <pre>
<span style="color:#000000;">&lt;LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; android:layout_width="fill_parent" </span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;android:layout_height="fill_parent"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; android:orientation="vertical" &gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; </span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&lt;TextView android:id="@+id/text_id"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; android:layout_width="wrap_content"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; android:layout_height="wrap_content"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; android:text="I am a TextView" /&gt;</span></pre>
   <pre>
<span style="color:#000000;">&lt;/LinearLayout&gt;</span></pre>
  </div>
  <p><span style="color:#000000;">Then finally create an instance of the Control object and capture it from the layout, use the following:</span></p>
  <div>
   <pre>
<span style="color:#000000;">TextView myText = (TextView) findViewById(R.id.text_id);</span></pre>
  </div>
  <p>&nbsp;</p>
  <h2>Android Event Handling</h2>
  <p>&nbsp;<strong>Event Listeners &amp; Event Handlers</strong></p>
  <table border="1" cellpadding="0" cellspacing="0" style="width:560px;" width="560">
   <tbody>
    <tr>
     <td style="width:103px;">
      <p><span style="color:#000000;"><strong>Event Handler</strong></span></p>
     </td>
     <td>
      <p><span style="color:#000000;"><strong>Event Listener &amp; Description</strong></span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">onClick()</span></p>
     </td>
     <td>
      <p><span style="color:#000000;"><strong>OnClickListener()</strong><br />
       This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. You will use onClick() event handler to handle such event.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">onLongClick()</span></p>
     </td>
     <td>
      <p><span style="color:#000000;"><strong>OnLongClickListener()</strong><br />
       This is called when the user either clicks or touches or focuses upon any widget like button, text, image etc. for one or more seconds. You will use onLongClick() event handler to handle such event.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">onFocusChange()</span></p>
     </td>
     <td>
      <p><span style="color:#000000;"><strong>OnFocusChangeListener()</strong><br />
       This is called when the widget looses its focus ie. user goes away from the view item. You will use onFocusChange() event handler to handle such event.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">onKey()</span></p>
     </td>
     <td>
      <p><span style="color:#000000;"><strong>OnFocusChangeListener()</strong><br />
       This is called when the user is focused on the item and presses or releases a hardware key on the device. You will use onKey() event handler to handle such event.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">onTouch()</span></p>
     </td>
     <td>
      <p><span style="color:#000000;"><strong>OnTouchListener()</strong><br />
       This is called when the user presses the key, releases the key, or any movement gesture on the screen. You will use onTouch() event handler to handle such event.</span></p>
     </td>
    </tr>
    <tr>
     <td>
      <p><span style="color:#000000;">onMenuItemClick()</span></p>
     </td>
     <td>
      <p><span style="color:#000000;"><strong>OnMenuItemClickListener()</strong><br />
       This is called when the user selects a menu item. You will use onMenuItemClick() event handler to handle such event.</span></p>
     </td>
    </tr>
   </tbody>
  </table>
  <p>&nbsp;<span style="font-size: 2.142857142857143rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">Event Listeners Registration:</span></p>
  <p><span style="color:#000000;"><span style="line-height: 1.5;">Event Registration is the process by which an Event Handler gets registered with an Event Listener so that the handler is called when the Event Listener fires the event. Though there are several tricky ways to register your event listener for any event, but I'm going to list down only top 3 ways, out of which you can use any of them based on the situation.</span></span></p>
  <p><span style="color:#000000;">· Using an Anonymous Inner Class</span></p>
  <p><span style="color:#000000;">· Activity class implements the Listener interface.</span></p>
  <p><span style="color:#000000;">· Using Layout file activity_main.xml to specify event handler directly.</span></p>
  <p><span style="color:#000000;">Below section will provide you detailed examples on all the three scenarios</span></p>
  <p>&nbsp;</p>
  <h2>Defining Styles</h2>
  <p><span style="color:#000000;">A style is defined in an XML resource that is separate from the XML that specifies the layout. This XML file resides under&nbsp;<strong>res/values/</strong>&nbsp;directory of your project and will have&nbsp;<strong>&lt;resources&gt;</strong>&nbsp;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.</span></p>
  <div>
   <pre>
<span style="color:#000000;"><span style="font-size: 1rem; word-spacing: normal;">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span>
</span></pre>
   <pre>
<span style="color:#000000;">&lt;resources&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; &lt;style name="CustomFontStyle"&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:layout_width"&gt;fill_parent&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:layout_height"&gt;wrap_content&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:capitalize"&gt;characters&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:typeface"&gt;monospace&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:textSize"&gt;12pt&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:textColor"&gt;#00FF00&lt;/item&gt;/&gt; </span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&lt;/style&gt;</span></pre>
   <pre>
<span style="color:#000000;">&lt;/resources&gt;</span></pre>
  </div>
  <p>&nbsp;</p>
  <h2>Using Styles</h2>
  <p>&nbsp;</p>
  <div>
   <pre>
<span style="color:#000000;">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span></pre>
   <pre>
<span style="color:#000000;">&lt;LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; android:layout_width="fill_parent"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; android:layout_height="fill_parent"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; android:orientation="vertical" &gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;<span style="font-size: 1rem; word-spacing: normal;">&nbsp;&nbsp; &lt;TextView </span><span style="font-size: 1rem; word-spacing: normal;">android:id="@+id/text_id"</span></span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; style="@style/CustomFontStyle"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; android:text="@string/hello_world" /&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;<span style="font-size: 1rem; word-spacing: normal;">&lt;/LinearLayout&gt;</span></span></pre>
  </div>
  <p>&nbsp;</p>
  <h2>Style Inheritance</h2>
  <p>&nbsp;</p>
  <p><span style="color:#000000;">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.</span></p>
  <p><span style="color:#000000;">Its simple, to create a new style&nbsp;<strong>LargeFont</strong>&nbsp;that inherits the&nbsp;<strong>CustomFontStyle</strong>&nbsp;style defined above, but make the font size big, you can author the new style like this:</span></p>
  <p>&nbsp;</p>
  <div>
   <pre>
<span style="color:#000000;">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span></pre>
   <pre>
<span style="color:#000000;">&lt;resources&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; &lt;style name="CustomFontStyle.LargeFont"&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:textSize"&gt;20ps&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; &lt;/style&gt;</span></pre>
   <pre>
<span style="color:#000000;">&lt;/resources&gt;</span></pre>
  </div>
  <p>&nbsp;</p>
  <p><span style="color:#000000;">You can reference this new style as&nbsp;<strong>@style/CustomFontStyle.LargeFont</strong>&nbsp;in your XML Layout file. You can continue inheriting like this as many times as you'd like, by chaining names with periods. For example, you can extend FontStyle.LargeFont to be Red, with:</span></p>
  <p>&nbsp;</p>
  <div>
   <pre>
<span style="color:#000000;">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span></pre>
   <pre>
<span style="color:#000000;">&lt;resources&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; &lt;style name="CustomFontStyle.LargeFont.Red"&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:textColor"&gt;#FF0000&lt;/item&gt;/&gt; </span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&lt;/style&gt;</span></pre>
   <pre>
<span style="color:#000000;">&lt;/resources&gt;</span></pre>
  </div>
  <p><span style="color:#000000;"><span style="line-height: 1.5;">This technique for inheritance by chaining together names only works for styles defined by your own resources. You can't inherit Android built-in styles this way. To reference an Android built-in style, such as&nbsp;</span><strong style="line-height: 1.5;">TextAppearance</strong><span style="line-height: 1.5;">, you must use the&nbsp;</span><strong style="line-height: 1.5;">parent</strong><span style="line-height: 1.5;">&nbsp;attribute as shown below:</span></span></p>
  <div>
   <pre>
<span style="color:#000000;">&lt;?xml version="1.0" encoding="utf-8"?&gt;</span></pre>
   <pre>
<span style="color:#000000;">&lt;resources&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; &lt;style name="CustomFontStyle" parent="@android:style/TextAppearance"&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:layout_width"&gt;fill_parent&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:layout_height"&gt;wrap_content&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:capitalize"&gt;characters&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:typeface"&gt;monospace&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:textSize"&gt;12pt&lt;/item&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;item name="android:textColor"&gt;#00FF00&lt;/item&gt;/&gt; </span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&lt;/style&gt;</span></pre>
   <pre>
<span style="color:#000000;">&lt;/resources&gt;</span></pre>
  </div>
  <p><span style="font-size: 2.142857142857143rem; font-weight: 700; letter-spacing: -1px; line-height: 30px;">Android Themes</span></p>
  <p><span style="color:#000000;">Hope you understood the concept of Style, so now let's try to understand what is a&nbsp;<strong>Theme</strong>. A theme is nothing but an Android style applied to an entire Activity or application, rather than an individual View.</span></p>
  <p><span style="color:#000000;">Thus, when a style is applied as a theme, every&nbsp;<strong>View</strong>&nbsp;in the Activity or application will apply each style property that it supports. For example, you can apply the same&nbsp;<strong>CustomFontStyle</strong>&nbsp;style as a theme for an Activity and then all text inside that&nbsp;<strong>Activity</strong>&nbsp;will have green monospace font.</span></p>
  <p><span style="color:#000000;"><span style="line-height: 1.5;">To set a theme for all the activities of your application, open the&nbsp;</span><strong style="line-height: 1.5;">AndroidManifest.xml</strong><span style="line-height: 1.5;">&nbsp;file and edit the</span><strong style="line-height: 1.5;">&lt;application&gt;</strong><span style="line-height: 1.5;">&nbsp;tag to include the&nbsp;</span><strong style="line-height: 1.5;">android:theme</strong><span style="line-height: 1.5;">&nbsp;attribute with the style name. For example</span></span></p>
  <div>
   <pre>
<span style="color:#000000;">&lt;application android:theme="@style/CustomFontStyle"&gt;</span></pre>
  </div>
  <p><span style="color:#000000;">But if you want a theme applied to just one Activity in your application, then add the android:theme attribute to the &lt;activity&gt; tag only. For example:</span></p>
  <div>
   <pre>
<span style="color:#000000;">&lt;activity android:theme="@style/CustomFontStyle"&gt;</span></pre>
  </div>
  <p><span style="color:#000000;">There are number of default themes defined by Android which you can use directly or inherit them using&nbsp;<strong>parent</strong>&nbsp;attribute as follows:</span></p>
  <div>
   <pre>
<span style="color:#000000;">&lt;style name="CustomTheme" parent="android:Theme.Light"&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; ...</span></pre>
   <pre>
<span style="color:#000000;">&lt;/style&gt;</span></pre>
  </div>
  <p>&nbsp;</p>
  <h2>Creating a Simple Custom Component</h2>
  <p>&nbsp;</p>
  <p><span style="color:#000000;">The simplest way to create your custom component is to extend an existing widget class or subclass with your own class if you want to extend the functionality of existing widget like Button, TextView, EditText, ListView, CheckBox etc. otherwise you can do everything yourself by starting with the<em>android.view.View</em>&nbsp;class.</span></p>
  <p><span style="color:#000000;"><span style="line-height: 1.5;">At its simplest form you will have to write your constructors corresponding to all the constructors of the base class. For example if you are going to extend&nbsp;</span><strong style="line-height: 1.5;">TextView</strong><span style="line-height: 1.5;">&nbsp;to create a&nbsp;</span><strong style="line-height: 1.5;">DateView</strong><span style="line-height: 1.5;">&nbsp;then following three constructors will be created for DateView class:</span></span></p>
  <div>
   <pre>
<span style="color:#000000;">public class DateView extends TextView {</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; public DateView(Context context) {</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(context);</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //--- Additional custom code --</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; }</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;<span style="font-size: 1rem; word-spacing: normal;">&nbsp;&nbsp; public DateView(Context context, AttributeSet attrs) {</span></span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(context, attrs);</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //--- Additional custom code --</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; }</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;<span style="font-size: 1rem; word-spacing: normal;">&nbsp;&nbsp; public DateView(Context context, AttributeSet attrs, int defStyle) {</span></span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; super(context, attrs, defStyle);</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //--- Additional custom code --</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; }</span></pre>
   <pre>
<span style="color:#000000;">}</span></pre>
  </div>
  <p><span style="color:#000000;">Because you have created DateView as child of TextView so it will have access on all the attributes, methods and events related to TextView and you will be able to use them without any further implementation. You will implement additional custom functionality inside your own code as explained in the given examples below.</span></p>
  <p><span style="color:#000000;">If you have requirement for implementing custom drawing/sizing for your custom widgets then you need to override&nbsp;<strong>onMeasure(int widthMeasureSpec, int heightMeasureSpec)</strong>&nbsp;and&nbsp;<strong>onDraw(Canvas canvas)</strong>&nbsp;methods. If you are not going to resize or change the shape of your built-in component then you do not need either of these methods in your custom component.</span></p>
  <p><span style="color:#000000;"><span style="line-height: 1.5;">The&nbsp;</span><em style="line-height: 1.5;">onMeasure()</em><span style="line-height: 1.5;">&nbsp;method coordinate with the layout manager to report the widget's width and height, and you need to call&nbsp;</span><em style="line-height: 1.5;">setMeasuredDimension(int width, int height)</em><span style="line-height: 1.5;">&nbsp;from inside this method to report the dimensions.</span></span></p>
  <p><span style="color:#000000;"><span style="line-height: 1.5;">You can then execute your custom drawing inside the&nbsp;</span><em style="line-height: 1.5;">onDraw(Canvas canvas)</em></span><span style="line-height: 1.5;"><span style="color:#000000;">&nbsp;method, where android.graphis.Canvas is pretty similar to its counterpart in Swing, and has methods such as drawRect(), drawLine(), drawString(), drawBitmap() etc. which you can use to draw your component</span>.</span></p>
  <p><span style="font-size: 1.714285714285714rem; letter-spacing: -0.5px; line-height: 24px;">INSTANTIATE USING CODE INSIDE ACTIVITY CLASS</span></p>
  <div>
   <pre>
<span style="color:#000000;">@Override</span></pre>
   <pre>
<span style="color:#000000;"> protected void onCreate(Bundle savedInstanceState) {</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp; super.onCreate(savedInstanceState);</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp; setContentView(R.layout.activity_main);</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp; <span style="font-size: 1rem; word-spacing: normal;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DateView dateView = new DateView(this);</span></span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp; setContentView(dateView);</span></pre>
   <pre>
<span style="color:#000000;"> }</span></pre>
  </div>
  <p>&nbsp;</p>
  <h3>INSTANTIATE USING LAYOUT XML FILE</h3>
  <p>&nbsp;</p>
  <p>Traditionally you use Layout XML file to instantiate your built-in widgets, same concept will apply on your custom widgets as well so you will be able to instantiate your custom component using Layout XML file as explained below. Here&nbsp;<strong>com.example.dateviewdemo</strong>&nbsp;is the package where you have put all the code related to&nbsp;<strong>DateView</strong>&nbsp;class and&nbsp;<strong>DateView</strong>&nbsp;is Java class name where you have put complete logic of your custom component.</p>
  <div>
   <pre>
&lt;RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"</pre>
   <pre>
&nbsp;&nbsp;&nbsp; xmlns:tools="https://schemas.android.com/tools"</pre>
   <pre>
&nbsp;&nbsp;&nbsp; android:layout_width="match_parent"</pre>
   <pre>
&nbsp;&nbsp;&nbsp; android:layout_height="match_parent"</pre>
   <pre>
&nbsp;&nbsp;&nbsp; android:paddingBottom="@dimen/activity_vertical_margin"</pre>
   <pre>
&nbsp;&nbsp;&nbsp; android:paddingLeft="@dimen/activity_horizontal_margin"</pre>
   <pre>
&nbsp;&nbsp;&nbsp; android:paddingRight="@dimen/activity_horizontal_margin"</pre>
   <pre>
&nbsp;&nbsp;&nbsp; android:paddingTop="@dimen/activity_vertical_margin"</pre>
   <pre>
&nbsp;&nbsp;&nbsp; tools:context=".MainActivity" &gt;</pre>
   <pre>
&nbsp;&nbsp;&nbsp; </pre>
   <pre>
&nbsp;&nbsp;&nbsp;&nbsp;&lt;com.example.dateviewdemo.DateView</pre>
   <pre>
&nbsp;&nbsp;&nbsp;&nbsp; android:layout_width="match_parent"</pre>
   <pre>
&nbsp;&nbsp;&nbsp;&nbsp; android:layout_height="wrap_content" </pre>
   <pre>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:textColor="#fff"</pre>
   <pre>
&nbsp;&nbsp;&nbsp;&nbsp; android:textSize="40sp"</pre>
   <pre>
&nbsp;&nbsp;&nbsp;&nbsp; android:background="#000"</pre>
   <pre>
&nbsp;&nbsp;&nbsp;&nbsp; /&gt;</pre>
   <pre>
&lt;/RelativeLayout&gt;</pre>
  </div>
  <p>&nbsp;</p>
  <h2>Custom Component with Custom Attributes</h2>
  <p>&nbsp;</p>
  <p><span style="color:#000000;">We have seen how we can extend functionality of built-in widgets but in both the examples given above we saw that extended component can make use of all the default attributes of its parent class. But consider a situation when you want to create your own attribute from scratch. Below is a simple procedure to create and use new attributes for Android Custom components. Consider we want to introduce three attributes and will use them as shown below:</span></p>
  <div>
   <pre>
<span style="color:#000000;">&lt;com.example.dateviewdemo.DateView</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; android:layout_width="match_parent"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; android:layout_height="wrap_content" </span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;android:textColor="#fff"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; android:textSize="40sp"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; custom:delimiter="-"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; custom:fancyText="true"</span></pre>
   <pre>
<span style="color:#000000;">/&gt;</span></pre>
  </div>
  <p>&nbsp;</p>
  <h3>STEP 1</h3>
  <p>&nbsp;</p>
  <p>The first step to enable us to use our custom attributes is to define them in a new xml file under<em>res/values/</em>&nbsp;and call it&nbsp;<strong>attrs.xml</strong>. Let's have a look on an example attrs.xml file:</p>
  <div>
   <pre>
&lt;?xml version="1.0" encoding="utf-8"?&gt;</pre>
   <pre>
&lt;resources&gt;</pre>
   <pre>
&nbsp;&nbsp; &lt;declare-styleable name="DateView"&gt;</pre>
   <pre>
&nbsp;&nbsp; &lt;attr name="delimiter" format="string"/&gt;</pre>
   <pre>
&nbsp;&nbsp; &lt;attr name="fancyText" format="boolean"/&gt;</pre>
   <pre>
&nbsp;&nbsp; &lt;/declare-styleable&gt;</pre>
   <pre>
&lt;/resources&gt;</pre>
  </div>
  <p>&nbsp;</p>
  <h3>STEP 2</h3>
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>
  <p><span style="color:#000000;">Your second step will be to read these attributes from Layout XML file and set them for the component. This logic will go in the constructors that get passed an&nbsp;<em>AttributeSet</em>, since that is what contains the XML attributes. To read the values in the XML, you need to first create a&nbsp;<em>TypedArray</em>&nbsp;from the<em>AttributeSet</em>, then use that to read and set the values as shown in the below example code:</span></p>
  <div>
   <pre>
<span style="color:#000000;">TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DateView);</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;</span></pre>
   <pre>
<span style="color:#000000;">final int N = a.getIndexCount();</span></pre>
   <pre>
<span style="color:#000000;">for (int i = 0; i &lt; N; ++i)</span></pre>
   <pre>
<span style="color:#000000;">{</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; int attr = a.getIndex(i);</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; switch (attr)</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; {</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case R.styleable.DateView_delimiter:</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String delimiter = a.getString(attr);</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //...do something with delimiter...</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case R.styleable.DateView_fancyText:</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; boolean fancyText = a.getBoolean(attr, false);</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //...do something with fancyText...</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp; }</span></pre>
   <pre>
<span style="color:#000000;">}</span></pre>
   <pre>
<span style="color:#000000;">a.recycle();</span></pre>
  </div>
  <h3 align="center">STEP 3</h3>
  <p>&nbsp;</p>
  <div>
   <pre>
<span style="color:#000000;">&lt;RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; xmlns:tools="https://schemas.android.com/tools"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; xmlns:custom="https://schemas.android.com/apk/res/com.example.dateviewdemo"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; android:layout_width="match_parent"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; android:layout_height="match_parent"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; android:paddingBottom="@dimen/activity_vertical_margin"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; android:paddingLeft="@dimen/activity_horizontal_margin"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; android:paddingRight="@dimen/activity_horizontal_margin"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; android:paddingTop="@dimen/activity_vertical_margin"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; tools:context=".MainActivity" &gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; <span style="font-size: 1rem; word-spacing: normal;">&lt;com.example.dateviewdemo.DateView</span></span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; android:layout_width="match_parent"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; android:layout_height="wrap_content" </span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp;&nbsp;android:textColor="#fff"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; android:textSize="40sp"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; custom:delimiter="-"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; custom:fancyText="true"</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;&nbsp;&nbsp; /&gt;</span></pre>
   <pre>
<span style="color:#000000;">&nbsp;<span style="font-size: 1rem; word-spacing: normal;">&lt;/RelativeLayout&gt;</span></span></pre>
  </div>
  <p>&nbsp;</p>
  <p><span style="color:#000000;">The important part is<em>xmlns:custom="https://schemas.android.com/apk/res/com.example.dateviewdemo"</em>. Note that<em>https://schemas.android.com/apk/res/</em>&nbsp;will remain as is, but last part will be set to your package name and also that you can use anything after xmlns:, in this example I used&nbsp;<strong>custom</strong>, but you could use any name you like.</span></p>
 </div>
</div>
<p align="center" style="margin-top:9.6pt;margin-right:0in;margin-bottom:12.0pt;
margin-left:0in;text-align:center;background:#00B050"><span style="font-family:&quot;Georgia&quot;,&quot;serif&quot;"><o:p></o:p></span></p>
Revert to this revision