Saturday, 28 June 2014

SIMPLE AND QUICK UNDERSTANDING OF ANDROID ENVIRONMENT AND DEVELOPMENT

UNDERSTOOD ANDROID DEVELOPMENT IN 48 HOURS.
Last month, we’ve been building an Android app. Fact is that i was never ever interested in android but later, because of unavailability of resources, i took some job on android development and realized it is just 48 hours of job to understand entire architecture and application development .Coming from a Microsoft-end perspective, I’ll take you through what it’s been like to work with Android and what you can expect as a new Android developer.

Introduction to Android development 

 
 

Android is operating environment based upon the Linux® kernel. Initially, the deployment target for Android was the mobile-phone arena, including smart phones and lower-cost flip-phone devices. However, Android's full range of computing services and rich functional support have the potential to extend beyond the mobile-phone market. Android can be useful for other platforms and applications. The Android platform is the product of the Open Handset Alliance, a group of organizations collaborating to build a better mobile phone. The group, led by Google, includes mobile operators, device handset manufacturers, component manufacturers, software solution and platform providers, and marketing companies. The first Android-capable handset on the market was the G1 device manufactured by HTC and provisioned on T-Mobile. The device became available after 9 months of speculation, where the only software development tools available were some incrementally improving SDK releases.

Here i will  give an introduction  of Android platform and learn how to code a basic like displaying  message in Android application.
The Android platform
Android is a layered environment built upon a foundation of the Linux kernel, and it includes rich functions. The UI subsystem includes:

  • Windows
  • Views
  • Widgets for elements such as edit boxes, buttons,lists, and drop-down lists
Android includes an embeddable browser built upon WebKit, the same open source browser engine powering the iPhone's Mobile Safari browser.
Android boasts a healthy array of connectivity options, including WiFi, Bluetooth, and wireless data over a cellular connection (for example, GPRS, EDGE, and 3G). A popular technique in Android applications is to link to Google Maps to display an address directly within an application. Support for location-based services (such as GPS) and accelerometers is also available in the Android software stack, though not all Android devices are equipped with the required hardware. There is also camera support.

ANDROID ARCHITECTURE
Android operating system is a stack of software components which is divided into five sections  as shown below in the architecture diagram.




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 Run-time
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 run-time 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
You will find all the Android application at the top layer. You will write your application to be installed on this layer only. Examples of such applications are Contacts Books, Browser, Games etc. An Android application consists of one or more of the following classifications:

Activities
An application that has a visible UI is implemented with an activity. When a user selects an application from the home screen or application launcher, an activity is started.
Services
A service should be used for any application that needs to persist for a long time, such as a workflow based application,network monitor.
Content providers
Content provider is something where data can be persisted/stored like database or xml. A content provider's job is to manage access to persisted data, such as a SQLite database. If you're developing big application, a content provider is the means of accessing your data.
Broadcast receivers
An Android application may be launched to process a element of data or respond to an event, such as click of button or receipt of a text message,
An Android application, along with a file called AndroidManifest.xml, is deployed to a device. AndroidManifest.xml contains the necessary configuration information to properly install it to the device. For example, whether an application requires access to the network? — this permission must be explicitly stated in the manifest file.
Required tools:What software tools you need to start android development

The easiest way to start developing Android applications is to download the Android studio or Eclipse IDE .We can do  Android development  on Microsoft® Windows®, Mac OS X, or Linux.I assumes you are using the Android studio. Android applications are written in the Java language, but compiled and executed in the Dalvik VM (a non-Java virtual machine). 
If you're a new Android developer, we recommend you download the ADT Bundle to quickly start developing apps. It includes the essential Android SDK components and a version of the Eclipse IDE with built-in ADT (Android Developer Tools) to streamline your Android app development.
Download Eclipse ADT
with the Android SDK for Windows
With a single download, the Eclipse ADT bundle includes everything you need to begin developing apps:
  • Eclipse + ADT plugin
  • Android SDK Tools
  • Android Platform-tools
  • A version of the Android platform
  • A version of the Android system image for the emulator
If you prefer to use an existing version of Eclipse or another IDE, you can instead download the stand-alone Android SDK Tools: After download SDK along with Ellicpse you can see below listed folders in your local system

Just click on ellipse folder and click on ellipse executable file and it will open ellipse IDE as show below.


src
This contains the .java source files for your project. By default, it includes an MainActivity.java source file having an activity class that runs when your app is launched using the app icon.

gen
This contains the .R file, a compiler-generated file that references all the resources found in your project. You should not modify this file.

bin
This folder contains the Android package files .apk built by the ADT during the build process and everything else needed to run an Android application.

res/
This is a directory for drawable objects that are designed for high-density screens.

res/layout
This is a directory for files that define your app's user interface.

res/values
This is a directory for other various XML files that contain a collection of resources, such as strings and colors definitions.

AndroidManifest.xml
This is the manifest file which describes the fundamental characteristics of the app and defines each of its components.
In the next section, you'll create a simple Android application.

Coding a basic application
This section provides a tour of building an Android application. The example application is about as simple as you can imagine: a modified "Hello Android" application. You'll add a minor modification to make the screen background color all white so you can use the phone as a   MySampleApp  .

Create Android Application 

The first step is to create a simple Android Application using Eclipse IDE. Follow the option File -> New -> Project and finally select Android New Application wizard from the wizard list. Now name your application as MySampleApp  using the wizard window as follows


Next, you create a simple application with a single activity, along with a UI layout stored in main.xml. The layout contains a text element you're going to modify to say Android   MySampleApp  . The simple layout is shown below.

Listing 1. MySampleApp  layout

The strings.xml file is located in the res/values folder and it contains all the text that your application uses. For example, the names of buttons, labels, default text, and similar types of strings go into this file. This file is responsible for their textual content. For example, a default strings file will look like as following file:.

Listing 2. strings.xml



The application has a Java source file called   MySampleApp  .java, as shown below.

The main activity code is a Java file MainActivity.java. This is the actual application file which ultimately gets converted to a Dalvik executable and runs your application. Following is the default code generated by the application wizard for MySampleApp :

The code is  directly from the New Project wizard:

  • It is part of a Java package called com.msi.  MySampleApp  .
  • It has two imports:
    • One for the activity class
    • One for the bundle class
  • When this activity is initiated, the onCreate method is invoked, passing in a savedInstanceState. Don't be concerned with this bundle for our purposes; it is used when an activity is suspended and then resumed.
  • The onCreate method is an override of the activity class method of the same name. It calls the super class's onCreate method.
  • A call to setContentView() associates the UI layout defined in the file main.xml. Anything in main.xml and strings.xml gets automatically mapped to constants defined in the R.java source file. Never edit this file directly, as it is changed upon every build.

Running the Application

Let's try to run our MySampleApp application we just created. I assume you had created your AVD while doing environment setup. To run the app from Eclipse, open one of your project's activity files and click Run icon from the toolbar. Eclipse installs the app on your AVD and starts it and if everything is fine with your setup and application, it will display finally following Emulator window:






SCENARIO: 

Create layout in android with some customized buttons.try to visualize in multiple screen sizes.The image appears proper on screen size 2.7 to 5.1. However buttons appear stretched on screen size 5.4 and greater .
Usually developers use multiple same images(like img01,img02..) , named them similarly of different densities and placed them correspondingly in the folders ldpi,mdpi,hdpi,xhpi.
Bad idea... how many images you will include? think of performance,optimization on priority.

SOLUTION

I was discussing with one of my colleagues and he was considering it as great challenge .wondering what is need of defining different layout folders(layout-land, layout-large, layout-small) if care will be taken in layouts consistently. The Android OS will take care of that(regarding images display). Only thing developer is going to take care is to do is add the <support-screens> in your manifest and below is the <support-screens>:

 <supports-screens android:resizeable=["true"| "false"]
              android:smallScreens=["true" | "false"]
              android:normalScreens=["true" | "false"]
              android:largeScreens=["true" | "false"]
              android:xlargeScreens=["true" | "false"]
              android:anyDensity=["true" | "false"]
              android:compatibleWidthLimitDp="integer"
              android:largestWidthLimitDp="integer"/>

I agree there are better practices to be fallowed along with this like
    Dont hard-code any layout parameters such as height,width etc..
    Dont use "px".Use "sp" for Text Size and "dp" for layout-width, layout-height etc.
    Prefer ScrollView wherever required for layouts as it supports for a singleView.
    Make use of RelativeLayout and LinearLayout and dont use AbsoluteLayout.

Screens supported

From Android 1.6 (API Level 4), there is support for multiple screen sizes and densities, reflecting the many different screen configurations that a device may have.
To simplify the way that you design your user interfaces for multiple screens, Android divides the range of actual screen sizes and densities into:

    A set of four generalized sizes: small, normal, large, and xlarge
    A set of four generalized densities: ldpi (low), mdpi (medium), hdpi (high), and xhdpi (extra high)

The different resource configurations that you can specify based on the space available for your layout are summarized in below table

characteristic Qualifier Description
Size small Resources for small size screens.
normal Resources for normal size screens. (This is the baseline size.)
large Resources for large size screens.
xlarge Resources for extra large size screens.
Density ldpi Resources for low-density (ldpi) screens (~120dpi).
mdpi Resources for medium-density (mdpi) screens (~160dpi). (This is the baseline density.)
hdpi Resources for high-density (hdpi) screens (~240dpi).
xhdpi Resources for extra high-density (xhdpi) screens (~320dpi).
nodpi Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.
tvdpi Resources for screens somewhere between mdpi and hdpi; approximately 213dpi. This is not considered a "primary" density group. It is mostly intended for televisions and most apps shouldn't need it—providing mdpi and hdpi resources is sufficient for most apps and the system will scale them as appropriate. If you find it necessary to provide tvdpi resources, you should size them at a factor of 1.33*mdpi. For example, a 100px x 100px image for mdpi screens should be 133px x 133px for tvdpi.
Orientation land Resources for screens in the landscape orientation (wide aspect ratio).
port Resources for screens in the portrait orientation (tall aspect ratio).
Aspect ratio long Resources for screens that have a significantly taller or wider aspect ratio (when in portrait or landscape orientation, respectively) than the baseline screen configuration.
notlong Resources for use screens that have an aspect ratio that is similar to the baseline screen configuration.
Summary:
In this article, you learned about Android at BASIC LEVEL. Hope, the example will help you to explore more of the Android platform.