My Android Notes

I am fascinated by Android platform and the potential it offers.
Recent Tweets @mohit_a

Final part of “Hello World” Android. In first two parts we have written a usable Android app and gained knowledge of layouts, events and resources. Now is the time to publish the app.

Part 1
Part 2
Unita’s Source Code 

Packaging the Application

Android phones are in multiple screen sizes. In the old days before iPhone and Android it used to be a nightmare to port one application build to multiple devices. A famous app had to be ported to other devices with different people hired or made responsible for separate devices.

Android has taken concrete steps to address this issue.  Keeping the layout definitions in separate from code as xml makes it effortless to support multiple screen sizes.

Since both our layouts and application logic is done, only thing remain is the image sizes. That’s it. All we need to do is to provide different set of images and Android will select that size. Same build. It is done by having a re-sized copy of the same image resource into different draw-able folders.

If you look into res folder, you will see drawable-hdpi, drawable-mdpi, drawable-ldpi. And Android by default has also created a ic_launcher icon image in these folders which will serve as the launcher icon.

We at the very least would like to have a different icon for Unita. After making your icon, have it copied to all the drawable folders by guidelines below

http://developer.android.com/guide/topics/resources/providing-resources.html

After this step, Unita is ready to be uploaded in Android market. Sign the application using the Android Tools as detailed in last post to prepare the apk

Publishing the App in the Market

This is it. Time to showcase Unita to the world. There are multiple Android Markets but Google’s Android Market is the biggest and is installed by default on most of the phones. 

Visit developer.android.com and signup for an account. There’s one time $25 registration fee. After following through the process you will have access Developer Console.

Click on the Upload Application button to go to launch the corresponding screen.

Most of the options are self explanatory.

Some interesting options are copy protection, country filter and content rating. Country Filter can be very helpful for getting feedback from a small user base, making necessary changes and then launch for everyone.

After uploading the application, listing can be seen on the Developer Console with bird-eye view on number of installs and feedback.

Here’s the published app.

Ending Thoughts

Hope this Hello World app will be helpful to others who are starting their journey with Android development. Android, although full of potential is really tough to master. It’s a powerful platform and even sky is not the limit on what all can be done with it. With every new release both hardware and software undergoes a dramatic change. Market in terms on number of users is growing very rapidly. Android truly does have potential to bring forward next wave of innovation in tech industry.

Link to Part 1 and source code for Unita 

Event Handling

For our simple unit converter, following UI events need to be handled.

  1. User selects the source unit from a Spinner control
  2. User selects to unit from the Spinner control
  3. User clicks a button to see the result of conversion

If you have a Java background, you must be aware of EventListeners (or similar design pattern). Android uses the same concept. UI elements sets its listener object. Listener instance is required to implement a particular interface type, who’s implementation will be handled by listener. It’s that simple.

Look at the following code; it’s an excerpt from UnitConveterActivity.

Spinner controls are from_units and to_units. Button is btnConvert. Since Unita is a simple one screen app, we can have Activity itself implement the required interfaces.

Spinner control requires setOnItemSelectedListener to be called and Button requires setOnClickListener to be called. Both of these methods take UnitConverterActivity instance as parameters and UnitConverterActivity implements requires interfaces.

As you can see that only one method takes care of both the Spinner control events. To differentiate between who’s the caller we compare parent parameter of onItemSelected method to the Spinner handle.

Completing the Application

With basic structure, design and event handling in picture; completing the application is primarily

  1. Writing app logic. 
  2. Populating Spinner controls and managing that data
  3. Ensuring object creation is minimum

Above goals are met using various code parts. 

converters package as most of the conversion logic. Spinner controls are managed by SelectionTracker class and Libaray class holds static instances of reusable objects.

After finishing the above steps do the following

  1. Run Lint : From Eclipse, right click on the project, select Android Tools -> Run Lint. Lint highlights common errors like missing resource in a layout, string being used as a literal etc.
  2. Ensure that all layout folders have appropriate resources. This is crucial in supporting multiple screens. For more information refer to http://developer.android.com/guide/practices/screens_support.html

On Device Testing

It is absolutely crucial that application is tested on Android device before publishing. More devices, the better. Simplest way to test your application on the phone

  1. Right click on project in Eclipse. From Android Tools, select “Export Signed Application Package”
  2. Follow the key generation wizard. Keep note of the keystore passwords as you be required to enter them every time you publish the application using the same keystore
  3. Download Astro File Manager on the target phone
  4. Transfer the .apk file generated in step (2) to the device’s SD card by USB connection
  5. Open the file in Astro and install it

Application will be installed on the device and can be launched like any other installed application.

In the next and final part, I be writing about publishing the application to the Google’s Android Market.

This blog is to chronicle the journey of writing some amazing apps. Before taking the first step towards that, how about a “Hello World”? Don’t worry this application will be completely usable and won’t just display a “Hello World” message :)

Edit: Here’s the code for Unita for easy reference. It is important to write the app yourself and only see the code if something is not clear in the post.

Task

  1. Develop a simple but usable Android application.
  2. Follow best practices
  3. Publish the app to the market

The finished app is named Unita, is a unit converter for multiple units and types. It can be downloaded from here.

I be writing everything in three posts. And covering the following 

  1. Part 1 - This post. Setup the environment for Android. Android Basics. Creating Android project. Using the layout editor for creating the Activity interface. Populating the data used by the interface elements.
  2. Part 2 - Event handling. Completing the application. On device testing. 
  3. Part 3 - Packaging the application. Publishing the app in the market

Setting up the development environment

Eclipse IDE, Android SDK, ADT Plugin for Eclipse. Following links should get you started well

http://developer.android.com/sdk/index.html
http://developer.android.com/sdk/eclipse-adt.html

After following the instructions detailed above, Eclipse should have option for creating an Android Project.

Android Basics

Android development is done using Java. Few things to take note of are

  1. Phone memory and processing power are limited. Network connectivity and QoS is not guaranteed 
  2. Android OS can prune any application anytime. Highest priority is given to the visible Activity and background Activities are stopped if system resources are required for the responsiveness of in use Activity
  3. Visible UI to the user is run using Activity aka a Form. Only one Activity can be in both receiving user input and visible state at a time i.e. it is possible for an Activity to share screen space with other activity but both of them won’t be responsive at the same time
  4. Android devices are vastly fragmented as compared to Apple’s iPhone
  5. Android development has structure and best practices in place to make the app available in multiple languages and on multiple screen sizes
  6. All meta, system, security information about the application is defined in AndroidManifest.xml. More on AndroidManifest
  7. Understand Activity Lifecycle

Congrats! With basics done we are good to go for writing our first Android app.

Creating Android Project

  1. Start Eclipse and select New -> Android Project. If option for “Android Project” is not present, select “Other…” (last option) and type “Android Project” in the new window.
  2. Give the new project name “Unita” -> Select target SDK -> Specify a package name. When you click finish Eclipse will create the project with default options.

Don’t get intimidated with the size of default structure created by Eclipse. Everything fits in the scheme of things. Notable folders

  • src: This is where application source code is kept
  • res/drawable-***: Screen and resolution specific icons, images, resources
  • res/layout: Activity layout. More on this in next section
  • res/values: By default values folder has a strings.xml where string data stored in the application should be stored for easy change and multilingual capabilities. More on this here. String and String Arrays are of interest for Unita

Using the Graphical Layout Tool

res/layout folder contains a main.xml. Layouts in Android are recommended to be created using XML. Although they can be created using Java but it is discouraged. Separating the GUI from code logic is good practice and allows modifying UI without touching the code elements.

Double clicking main.xml should open “Graphical Layout” tab. You can also view and modify the raw XML from the next tab or open the file using XML editor.

You can easily create an interface like this. Also default theme can be set from here, which is set to Theme.Light

Unita layout


We are using simple UI elements. Labels, EditText, Spinner and Buttons. Do take note of Spinners as we be writing some interesting event handling code for them.

Populating Data Used By Interface Elements

It be great if first Spinner (the pull down list) can have list of all available units and on selection of one of the unit second Spinner can be filled with compatible units. To accomplish both these tasks we need to define units as a group and String Arrays help achieving that.

For e.g. in strings.xml define weight units as 

    <string-array name=”units_weight”>
        <item>Gram</item>
        <item>Kilogram</item>
        <item>Milligram</item>
        <item>Grain</item>
        <item>Dram</item>
        <item>Ounce</item>
        <item>Pound</item>
        <item>Pennyweight</item>
        <item>Troy Ounce</item>
        <item>Troy Pound</item>
    </string-array>

Assuming all the unit groups are defined as above, we can concentrate on the application code in src folder.

Eclipse must have created one Activity UnitaActivity in the base package. Open that file and concentrate on onCreate method. Please read the activity lifecycle from the link in Android Basics section.

We can refer to first Spinner, who’s id is from_units in main.xml as

Spinner from_units = (Spinner) findViewById(R.id.from_units);

To pre-populate it we need to use ArrayAdapter. Create an ArrayAdapter like

ArrayAdapter source = new ArrayAdapter<String>(context,

android.R.layout.simple_spinner_item);

android.R.layout. has various default layout templates which affects the presentation of elements.

After populating the ArrayAdapter, we can attach it to from_units as 

from_units.setAdapter( source )

R.array.units_<unit_name> will return a Java string array String[]. 

These values can be added to ArrayAdapter by calling add() method and passing the string value to it.

If you have followed up so far, you should have a Spinner which is populated with the units defined in strings.xml as String Arrays.

Number of Android devices is increasing rapidly, so is the number of Android Markets. Unlike reverent Apple Appstore there is no “one” central place for Android apps but many.

It does seems like a step backward but it could also have some advantages

1) An Android Market offering only apps for certain geography, language or genre could benefit users seeking only certain kind of apps.
2) Certain hardware publishers can offer specialized app market for their own devices thus offering more targeted content.

In lack of better example, Mikandi.com is an Android Market offering only adult apps. Amazon’s Kindle Fire which uses Android platform has Amazon Appstore for apps.

Following is list of some famous markets 

1. Google Android Market: Also known famously as Android Market is most widely deployed as an app on phones using Android platform. Google offers hosts of services including multi currency payments, country filters etc. Over a billion apps are downloaded from Android Market. Payments are split 70:30 between developer, payment processors and carriers. Google does not keep any share. Developers also has to pay one time US$25 registration fee.

2. Amazon Appstore: Appstore for Kindle Fire, which runs on Android platform. Amazon Appstore is currently only available in US. Store has a unique “Test Drive” feature which allows users to run the Android app in their web browser for half an hour. Developer pay share is 70% of the sale price or 20% of the list price, whichever is higher. List price is the price at which application is listed in the store. If a developer offer a promotion which lowers the sale price for the app for a certain time, payment rules as stated above comes in picture. Amazon Appstore has US$99 per year developer fee.

3. GetJar: Older than both Google and Amazon markets, GetJar offers apps for multiple mobile platforms. GetJar is very famous and offers only free apps. There’s no developer fee. Many popular apps are made available free at GetJar for gaining traction. GetJar also offers social features by allowing users to see what their Facebook friends have downloaded.

4. SlideME: An Android only market. SlideME is a relatively new market offering 95% share with developers. There’s no registration fee. According to their website, SlideME now is pre-loaded to 120 OEMs worldwide. 

There are many more Android Markets and list is set to grow with new markets being announced almost every month. Each one has its own strengths. It’s debatable whether this market fragmentation benefits users but it surely increases work for developers in multitudes. Uploading, maintaining, updating, working on the feedback, different payment channels, pay split. These are some of the factors which needs to be addressed.

Hello there,

Android is rapidly growing platform. With a huge fan following, it offers unparalleled potential. I intend to learn all the nitty-gritties of Android development. This blog is where I want to share my learning and also get feedback.

Asker tumblrbot Asks:
ROBOTS OR DINOSAURS?
droidride droidride Said:

ROBOTS