Creating First Activity

If you have followed previous tutorial, you must be ready to create the first Activity.

Before starting let’s have a quick overview of what activity is in android developer site.

Well,  let’s create our first Activity. Create a package and class in src folder as shown in the picture below.

act-300x136

 

Lets turn it into an Activity by extending it from Activity class and creating onCreate() method. It is the first method that gets executed when the activity starts.

package com.technoguff.tutorial;

import android.app.Activity;

import android.os.Bundle;

public class HelloWorldActivity extends Activity{

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

}

}

Now our activity is created. Next thing we have to do is notify the AndroidManifest file that we have an activity which needs to be started when the app is launched.

Open AndroidManifest File and select Application Tab, click on add and choose Activity. For name Browse the existing HelloWorldActivity.

 

manifest-300x109

 

Similarly selecting the Activity node add new node called Intent Filter, then under intent filter node add new nodes called Action and category with values Main and launcher respectively. We will get familiar with intent-filters, categories in this tutorial. Here category LAUNCHER insures that the launcher icon is created for this activity and MAIN action is for running this activity when the app starts.

 

main-300x111

 

 

Alternatively, this can be done directly editing the code from AndroidManifest.xml tab by typing following code inside the Application tag.

 

<activity android:name=“com.technoguff.tutorial.HelloWorldActivity”>

<intent-filter android:label=“@string/app_name” android:icon=“@drawable/ic_launcher”>

<category android:name=“android.intent.category.LAUNCHER”/>

<action android:name=“android.intent.action.MAIN”/>

</intent-filter>

</activity>

 

Now we have successfully created our first activity and our application is ready to run. But we don’t yet have any ui elements added. If you run your application at this stage you will just see an empty activity. So let’s add a Textview to our Activity.

 

package com.technoguff.tutorial;

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

 

public class HelloWorldActivity extends Activity{

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

TextView tvHello = new TextView(this);

tvHello.setText(“Hello World”);

setContentView(tvHello);

}

}

 

When we run the app we can see the textView displaying “Hello World”. As shown in the picture below.

screen-187x300

 

 

Android project

Now our environment is all set up and we are ready to get started. Let’s create our first Android Application.

Select Android project from File >> New >> Android Project

Or click New Android Project icon on toolbar.

new-300x118

Then provide Project name, package name and Build SDK and minimum API level.

project-300x275

Then in the next window select whether to create MainActivity or not. If you select create activity option Ecliplse will create sample Activity for you. You can create launcher icon too. But here we will create activities manually. We will see the use of  new project wizard in detail when we get familiar with the basic project structure.

folders-174x300

When you click finish button your first android project will be ready for you. Let’s learn about the project structure . Under your project folder you can see following folders.

1)      src:  It is the java source folders. We will create our java packages and classes here.

2)      gen: This folder contains auto-generated files  named BuildConfig.java and R.java.

Buildconfig.java contains build configuration information while R.java contains all the resource classes. All the resources you create or import in the project will be mapped using this R class.

3)      assets : assets folder holds the resources required for your project. This is similar to the resources placed in res folder but unlike the files in res folder the assets are not mapped using  R class. We have to access the assets using AssetManager Class.

4)      bin: This folder contains your binaries. The build files are located here. your application package all the resources and android manifest are locate in this folder which are used to build the final apk file.

5)      res: This folder holds the resources used in android project. Resource folder primarily contains drawables for different devices based on the resolution, xml layouts, and values. But we can add other resources like menu, xml etc later.

6)      AndroidManifest.xml  : this file contains all the information about your application. It is the entry point of any android application. Android manifest defines which activities exist in the application and which activity needs to run first. It also stores all the information about required permissions, data providers, services etc.

Enable Hardware Acceleration in AVD

We installed set up AVD in the last tutorial. You might have noticed that the emulator was not responding smoothly. Since API Level 15 android emulator has got this new feature called Graphics acceleration.  Graphics acceleration feature has made the emulators a lot more responsive. Graphics acceleration uses computer’s graphics hardware, specifically the GPU. Requirement for using GPU emulation is,

Android SDK Tools, Revision 17 or higher

Android SDK Platform API 15, Revision 3 or higher 

In order to enable graphics acceleration follow following steps.

Edit emulator.

In Hadrware section click New Button.

Select GPU emulation from property droipdown.

Then change the value to Yes.

Working with Android emulator

We set up basic working environment in the previous tutorial.  ADT plugin for eclipse that we installed comes with cool android emulator  which we can use during development. Android emulator also known as Android Virtual Device (AVD) is very useful tool for the developers for testing various features in different API levels of Android. Different devices with varying screen sizes can also be created for testing the user experience. In this section we will see how we can make the best of android virtual devices.

Let’s set up our emulator for development.

Click on small emulator icon on the eclipse toolbar to set up android virtual devices(AVD)

Provide AVD name, Target and SDCard Size and click create.

Select desired target for your virtual device. You can try creating different virtual devices for different android API levels for testing purposes.

Device with different resolutions can be created in order to test your app for various screen sizes.

You can now start and see the emulator running. You are ready to start writing your application.

Beginning Android development

This tutorial is meant for absolute beginners who want to start developing android development. It will help you to start with basic concepts. Let’st start with android development tools setup.

1)      Download JDK

2)      Download Eclipse for Java Developers

3)      Download Android SDK using Following URL

 http://developer.android.com/sdk/index.html

Install the base sdk installer package .

4)      Download ADT Plugin for eclipse

Is available as a zip file as well as through eclipse update.

Using Zip file

From menu select Help>> Install New Software

Use archive button and browse to the directory where downloaded zip file is located.

Check  “Developer Tools” check box and Click Install to proceed.

You may require to restart the eclipse after installing ADT plugin.

 

Downloading ADT  from eclipse  update:

From menu select Help>> Install New Software

Then use the download archive or the url  https://dl-ssl.google.com/android/eclipse/

Try http://dl-ssl.google.com/android/eclipse/ if HTTPS url doesn’t work.

Check  “Developer Tools” check box and Click Install to proceed.

You may require to restart the eclipse after installing ADT plugin.

 

 

Then Setup SDK path by selecting  “Windows >> Preferences”

From eclipse menu. Then browse to  Android SDK directory.

 

You can download required SDK packages bby using “Android SDK Manager” icon on the toolbar.