Thursday, December 12, 2013

Working with Android Studio

Android SDK: Working with Android StudioThe goal of this tutorial is to learn about Android development, including the tools and resources you need to in order to start creating apps. In the last part we became acquainted with the Eclipse IDE. Since the platform began, Eclipse has been the main supported development environment used to create Android apps.
 Eclipse is still the recommended IDE for Android, but this is set to change with the new Android Studio IDE. Android Studio is not yet complete, but it is available as an early access preview. This new IDE is designed specifically for Android development and based on IntelliJ IDEA in conjunction with JetBrains. When it is complete, Android Studio will improve several aspects of its Android development, not the least of which is the process of designing UIs to support the range of Android devices in use. In this tutorial we will install and explore Android Studio.

Introduction

Although Eclipse should still be your main focus for developing Android apps, it is worth spending some time with Android Studio, particularly if you intend to be involved in Android development long term.  

1. Installation

Step 1

As you have seen, the ADT Bundle download includes the Eclipse IDE for Android development. To try out Android Studio, download it from the Android Developer site. There are versions for Windows, Mac, and Linux, so choose the one for your operating system.

Step 2

On Windows, install Android Studio by running the executable “.exe” file that you downloaded. Follow the instructions to complete the installation. On Mac OS X, install by opening the DMG file you downloaded, then drag Android Studio to your Applications folder. On Linux, you need to first unzip the downloaded directory. Inside the unpacked folder, browse to the “bin” directory. You can execute Android Studio from here by running the “studio.sh” file.
Android Studio

2. Start a Project

Step 1

Let’s get to know Android Studio by starting a project as we did with Eclipse. When you start Android Studio for the first time, you will be presented with a welcome screen that offers a few options. Choose “New Project”.
Android Studio Welcome
As you can see, the process of creating a new project is very similar to Eclipse. In the New Project screen, leave the default options for naming the application elements and API level support and then click “Next”.
New Project
In the launcher icon config screen, leave everything as-is and click “Next”.
New Icon
In the Activity config screen, let Android Studio create a new blank Activity for you by leaving “Blank Activity” selected and click “Next”.
New Activity
Leave the Activity and layout text-fields with the automatically populated file names and click “Finish”.
New Project Finish
Android Studio will build your project and create a folder for it in the workspace. Like Eclipse, Android Studio manages the files for a project in a single directory and they are all saved inside a folder named “AndroidStudioProjects” onto your system.
New Project Build
Tip: Android Studio can be pretty unstable. If anything goes wrong when creating your first project, try updating the software by choosing “Help” and “Check for Updates”.

3. The Environment

Step 1

Let’s now look at the elements of the Android Studio environment with your new project open. When it creates a new project, Android Studio opens the layout and Activity files for editing, as well as loads a preview of your app’s UI on a virtual device screen. As you can see, the default window layout is slightly different than Eclipse.
Android Studio Workspace
Releases of Android Studio vary and will likely continue to, but at the time of writing, by default Android Studio initially displays an editor area for your Java and XML files together with the preview area to the right of the screen. Remember that with Eclipse, we had an area to the left of the window in which we could explore the project structure and open files to edit. In Android Studio you can also do this. If you look above the editor area you will see links to the project directories. Double-click on “MyApplicationProject” to open the project directory in a dedicated view.
Android Studio Project
Experiment with the project view by expanding the folders that appear within it. Expand “MyApplication” to access the contents of your new project. You will find the items we will work with as we continue to learn Android development inside the “src/main” directory. We will be particularly interested in the content of the “java” and “res” folders. Notice that there are quick contextual links above the editor to locations within the project structure relative to the current file.
The files Android Studio should have opened when you created your project are the Activity file, which you will find in the package folder inside “java”, and the layout file, which is stored inside the “layout” folder in “res”. Notice that when a layout file is open in the editor, you can switch between design and text views, which will allow you to build and edit your UI elements both graphically and via code. When the Design view is open, to the right you will see the Component Tree, a structural overview of the layout. When the text view of a layout is open, to the right you will see the preview of the layout.
Android Studio Design

Step 2

Although you may not yet be familiar with the structure of an Android project, once you learn a little more you will see that Android Studio structures the content of your app project’s a little differently than Eclipse. The files you are most likely to work with are stored inside “src”. This is because the build system in Android Studio is Gradle-based. Don’t worry too much about this difference if you are working in both Eclipse and Android Studio in the future, as the differences are unlikely to affect you. Just get used to the fact that the source files you work with will be located in different places in the two IDEs.

4. Project Interaction

Step 1

One of the better features in Android Studio is that many of the tools you need are accessible via the buttons in the toolbar. Take a moment to scroll over these now. Notice that there are quick links to launch various utilities including the AVD Manager, SDK Manager, and the Monitor that includes debugging tools.
Android Studio Buttons

Step 2

Like Eclipse, Android Studio provides a range of views you can use to interact with the contents of your projects. Select the “View” and “Tool Windows” menu to see the range of options. Clicking these toggles them in-and-out of visibility. Views you may find useful include the Structure view, which is similar to the Outline view in Eclipse and the Event Log view, which displays system output messages.
One view you may find particularly useful once you are attempting to develop and debug apps is the “Android” view. Open it now from the “Tool Windows” menu. This view displays information about actual and virtual devices, together with the LogCat for outputting debugging messages.
Android Studio DDMS

5. Updates

Step 1

As with Eclipse, you need to keep your Android Studio installation up to date to get the best use out of it. You can use the “Help”, “Check for Update” tool to keep the software itself up to date regularly. This is advisable since Android Studio is under constant development, with patches often released to address issues you may be facing.

Step 2

As we’ve seen with Eclipse, as well as keeping the IDE up to date, you should check for Android updates regularly as well. In Android Studio you can do this by clicking the “SDK Manager” button you see in the toolbar.
SDK Manager
The process of keeping the Android SDK up to date is exactly the same as with Eclipse.

Tip: For more tips on Android Studio see the IntelliJ IDEA documentation.

Monday, October 28, 2013

First Android Hello World Program


android

If you are a software developer you should at least write a “Hello World!” in android. That’s what we are going to do in this article. We know how android gets related to java and so to a java blog. Oops! I am not mentioning about the Oracle’s case against Google claiming the presence of java code in android.

As always, Hello World is the easiest step you can keep forward in learning a language. I would say, writing Hello World is not about learning the language. It is all about learning the setting up of the development environment, how to compile, interpret / execute.

IDE is everything in Hello, World

If you have completed setting up the development environment, then Android Hello World is simple. If you are using Eclipse as your IDE, setting up it for Android is also simple. Its about just installing a plugin. If you are not using Eclipse, don’t worry there are nice documentation available in android’s site.

Android Hello World requires the following steps

  1. JDK 5 or above (JRE alone is not sufficient) installation
  2. Eclipse Ganymede or above installation
  3. Android SDK Installation
  4. Android Development Tools plugin installation
  5. Android Emulator – Android Virtual Device (AVD) Creation
  6. New Android Project Creation
  7. Create Hello World Android source
  8. Run Android Hello World
A word of caution. If you are going to do this Hello World Android from your office computer, get necessary permissions from your manager. Since, you may be behind some proxy/firewall the software update sites might be blocked. You may get frustrated setting up the IDE.

If you are a java guy, you should be already having some JDK installed in your computer. Just check the version for compatibility. If you have a older version of Eclipse, download the latest version. No harm in having multiple Eclipse versions. Android SDK and plugin installation, just follow the instructions given the URL I have given above. They are more than sufficient.

After you install the ADT plugin, restart the eclipse. Then in Eclipse, Window -> Preferences (in left panel select Android) then give your SDK installed location in the right side panel.

Android Emulator

If you have an android mobile and you want to execute your Hello World in that, there is a time for it. First master yourselves with the basics and you can use the Android Emulator. Always try first with the emulator before going to the real mobile. It will save you lot of time.



Click the android icon (with a white down arrow) from the Eclipse toolbar. I have show that icon in the below image. Then click the ‘New’ button and create a AVD. Finally it should look like the window in my image.

Android Hello World Project Creation

Right click in your Eclipse’s Package Explorer and New -> Android Project. Give a project name(Ex: AndroidHelloWorld), then select the Build Target. The build target should match the api version you have given at the time of AVD creation. Then fill the properties panel also.



Now the HelloWorld android project is ready to use. Open the HelloWorld.java from the project source. You should have a HelloWorld class source code similar to the below.

package com.javapapers.android;
 
import android.app.Activity;
import android.os.Bundle;
 
public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}
 
Notice that you don’t have a main method. It is not needed. This HelloWorld is based on the Activity class and onCreate method will be called on initiation.
Now lets add our HelloWorld part to it. Edit the code and add the following snipped to it. Import TextView also.

TextView tv = new TextView(this);
tv.setText("Hello World!");
 
then set the contentView to the TextView you have created

setContentView(tv);
 
Now your Android Hello World should look like
 
package com.javapapers.android;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("Hello World!");
        setContentView(R.layout.main);
    }
}

Run your Android Hello World App

Right click on the project ‘Run As -> Android Application’
The AVD (emulator) will get started and it will take a while to respond. AVD needs that time to initialize itself. Wait for 2 to 3 minutes. Keep an eye on the console, it will give you some background process status information. Then you see “Hello World!”
.

Friday, October 25, 2013

Android Development Environment Setup

This article is a recap and will provide detailed step by step instructions to setup the Android development environment. If you have already got your setup done, this tutorial has got nothing new.

Steps for Environment Setup

  1. Install JDK
  2. Setup Eclipse IDE
  3. Install Android SDK (Manager)
  4. Install Android ADT Eclipse Plugin
  5. Install Android Packages
  6. Android Virtual Device Manager Setup

1. Install JDK

JRE alone is not sufficient, we should have the JDK setup done. Lets download and install latest version of JDK. No harm in having latest version of software.
 
Java Platform, Standard Edition is what we should have. We need not have NetBeans as we are going to use Eclipse. After installation its better to set the Path variable.

JDK Setup

If the JDK setup is done successfully, we should get the following in command prompt,

jdk install

2. Setup Eclipse IDE

Eclipse comes in a zip file. All we need to do is, download the right package and just unzip it to a location in hard disk. It is better to download “Eclipse for Java EE Developers”. Even if you are not doing any EE development, download this version. Since it comes bundled with good set of plugins that might be handy in some situations.

Download Eclipse

Download the zip file bundle and unzip it.

3. Install Android SDK (Manager)

Android SDK comes bundled with Eclipse. We do not need that version, since we already have Eclipse. We need to download and install only the SDK version which is approximately 85 MB as of now.
http://developer.android.com/sdk/index.html
Adroid SDK Download
Download SDK, install – click next, next, … till Finish.

Android SDK Install
Android SDK Install 2

After you click ‘Finish’, this will start the SDK Manager and you will get a new window, it is to be used for packages installation. Keep that as it is, lets complete the ADT Eclipse plugin installation and come back to this.

4. Install Android ADT Eclipse Plugin

If you are in a company network, mostly you will be behind a proxy server / firewall. There might be restrictions on downloading and installing software. You need to check with your IT admin, raise request, follow up with them and get it done.


Most of the cases, IT guys will not understand the importance of these and they will talk like you are about to steal the world bank data. I know how much hell of a work it is. If you are at home, then these download and installation will be a cakewalk.
Install ADT Plugin

Click Next,

Install ADT Plugin 2

Click next and Finish.

This might take lots and lots of time, based on our internet connection speed. If you are in BSNL connection, go have a good sleep and come back later. During this install, the system firewall will ask for permission, give ok ok.
Restart Eclipse after its done and set the preferences,

Android SDK Preference

Android SDK Preference 1

5. Install Android Packages

After installation of Android SDK Manager, we will “Start SDK Manager” and get the following screen,

Android Package Installation

In the above screen, I have selected certain check-boxes (make sure you have minimum selected all those). Presently I am installing packages for development for Android version 4.3 (API 18). If you are going to develop for older version of Androids, then select the respective versions. If you are not sure which version to choose, just select the latest version as I have done now. On click of install, we will get below, ensure all are in green tick mark, if not select that item and ‘Accept’ license.

Android Package Installation 2

Now click ‘Install’, this is also going to take lot of time.

6. Android Virtual Device Manager Setup

This step is required to test the apps we develop within Eclipse using the Android virtual device. After Android ADT plugin installation in Eclipse, we will get couple new icons in the Eclipse toolbar. In the top left we can see those. One icon is for ‘Android Virtual Device Manager’, on click we will get the following screen,

Android Virtual Device Manager Setup

Click ‘New’, give the following configuration and Ok,

AVD Configuration

Now we are ready to create our first Android mobile application. World is going Android way, so it is good to learn Android development.