dcsimg
December 10, 2016
Hot Topics:

Getting Started with Android Using Java

  • July 1, 2015
  • By Manoj Debnath
  • Send Email »
  • More Articles »

Android is a free, open source operating system initially powered by Dalvik VM, with a recent revamp to ART for hand-held devices such as phones, tablets, and so forth. The Android SDK basically provides a framework for creating interactive clients using the Java language. Java already has quite a few UI class libraries such as SWT, Swing, AWT, J2ME Canvas, and so on, but none of them gained such wide acceptance as Android, especially in the arena of hand-held devices. This article shall provide instruction to get a first taste of Android Programming in Java in a simple manner.

The Camaraderie

Android reshaped Java's client-side UI programming in hand-held devices. This camaraderie of Java and Android made switching between Java and Android almost a walk in the park; rather, there is no switching at all. It's all Java programming with another class library called Android. However, there is a twist. Programming a mobile device is significantly different from programming a PC. Here, the devices are different and so is the programming structure. Hand-held devices come in a variety of shapes and sizes; there is no mouse control although a touch screen is there, text input is different, and there are many peripherals such as a motion sensor, GPS unit, cameras, and multiple radios with a chronic concern of power consumption. Processing power and memory capacity are evolving so fast and directly impacting on the battery life of the device. This is one of the prime reasons for Google's decision to replace the underlying Dalvik VM to ART.

Android at a Glimpse

  • Android is a free and open source mobile operating system build by Google.
  • Google maintains Android and provides tools and updates from time to time.
  • Because it is free, anybody can modify and build apps for free.
  • Device manufacturers use Android OS and build special hardware around it or implement a custom functionality specific to their device.
  • Android app runs on a VM; as a result, one can build apps on multiple platforms, such as Mac, Windows, Linux, and so on, irrespective of the concern of underlying hardware.
  • Recently, to increate performance of Android apps, Google introduced ART that uses AOT compilation, replacing the JIT compilation of Dalvik VM.

Tools and Basics

Here are the tools to get your hands dirty.

  1. Java Development Kit
  2. Android SDK
  3. Integrated Development Environment (IDE)
    1. Android Studio, a hassle-free, all in one, feature-rich IDE powered by Intellij, specifically for Android programming. Downside is slow, very memory intensive and a long way to go to compete with IDE like Eclipse at least in view of performance.
    2. Eclipse, popular IDE for Java programming, can be tuned for Android programming as well.
  4. Eclipse ADT plugin for Android with installation instructions.

Set up Eclipse so that it finds the Android SDK from Windows --> Preferences.

Java1
Figure 1: The Preferences window

Resources

Test Drive

To test that everything is working just fine, let's create an Android project in Eclipse.

  1. Click File --> New --> Other... and select Android Application Project, Next.

Java2
Figure 2: The New Project window

  1. Provide the Application Name, ProjectName, and Package Name as follows:
    • Application Name: TestAndroidApplication. This is the application name the user will see.
    • Project Name: TestAndroidApplication. This is the name of the Eclipse project.
    • Package Name: org.mano.testandroidapplication. This creates a Java package namespace that uniquely identifies packages in an application.

Java3
Figure 3: The New Android Application window

Then, keep clicking Next, accepting all the defaults, and lastly click Finish.

  1. Now, expand the view of the project hierarchy and observe the various parts of the Android project.

Java4
Figure 4: Viewing parts of the Android project

The src folder contains the package and a Java file named MainActivity.java, or any other name you have entered in the project creation wizard. The source contains the following code:

package org.mano.testandroidapplication;

import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.os.Bundle;


public class MainActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar
      // if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

This is the minimal code skeleton automatically generated by Eclipse. Also, observe that <Project>/res/layout contains activity_main.xml. This file contains the UI rendering definition in XML format. Double-click this file to open and observe it in graphical layout design format and XML format. Graphical components can be dragged and dropped from the Pallete to the activity UI screen. Our project here is very minimal and does not need any modification in this file.

Java5
Figure 5: Dragging to the Palette

Note: Sometimes, there is a rendering problem when viewing the device screen. In such a case, change the rendering layout value to the lowest API version or experiment with different versions you have. If nothing works, try downloading lower version APIs from Android SDK Manager.

Java6
Figure 6: The Android SDK Manager screen

Creating an Android Virtual Device (AVD)

An Android program can be run on an actual Android device or we can run it in an emulator called AVD. AVD is an emulator that emulates a device running the Android operating System in a computer. We can set up a virtual device with the help of the AVD manager. We can set up parameters to configure AVD so that it uses a system image of the specific version of the Android operating system. The configurations are such as screen size, memory size, hardware characteristics, and the like.

Java7
Figure 7: Creating an AVD

Once the emulator is created, start the emulator from the AVD Manager, and then run the Android project as an Android Application. The output should be as follows.

Java8
Figure 8: The desired output

The AVD emulator has a bad reputation of being slow and very resource intensive. It takes several minutes to load (sometime 10 minutes!) and at times seem unresponsive. Patience is a virtue at least when running an AVD emulator. Once loaded, it is a good idea to keep the emulator up and running so that the next time when you run the application again after a few changes in the code, the emulator stays in a ready state to run the application.

Note: There is a way to stimulate AVD emulator response with the help of Intel HAXM, provided the PC hardware supports Virtualization (VT-x). For more information, visit Installation Instructions for Intel® Hardware Accelerated Execution Manager.

Conclusion

When learning a programming language, running an error free code is the first victory. It does not matter how simple the code is. Because Android code is akin to Java, a working knowledge of Java programming is a must. Running Android code in an AVD emulator may be a good idea initially for learning, but a serious programmer must have a real Android device attached to the PC while testing an app. This is necessary to not only get a real feel but also to get rid of the patience test. An emulator is fine in its own right, especially only when there is no other choice.


Tags: Java, Google, Android, VM, ART+COM, Swing, AWT, Dalvik VM, SWT, J2ME Canvas




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel