Home Eclipse Writing your first Android app

Writing your first Android app

by admin

Writing your first Android app


The purpose of this post – on the one hand to share my successful experience in the development of applications for the Android platform and on the other hand to contribute to the market development of software for this wonderful and thriving platform at the expense of (without false modesty I will say) probably you who read this post.In the network, of course, you can find materials on developing an application "slightly more complicated than helloworld", but as a rule they are fragmented and do not describe the various small pitfalls.In this post we will look at the full cycle of application development, from bare computer to the finished apk-file.Below are screenshots.

Preparing for development

No special actions and manipulations are required in order to prepare for development. I will describe step by step what you need to do. I have Windows XP, so all of the following should apply to that operating system, although the actions without any major changes can be applied to other operating systems of the Windows family and even Linux.

  • Installing the Eclipse development environment
  • Installing ADT plugin for Eclipse
  • JDK and JRE installation
  • Installing Android SDK
Installing the Eclipse development environment

This is pretty straightforward. We go to Downloads at official site and download the Classic version. Then just unpack the archive wherever you want, I decided to do it in the honorable directory C:\Eclipse

ADT plugin installation for Eclipse

Start Eclipse, and open the plugin installation dialog through the menu Help → Install new software In the Work with field, enter the plugin download address. dl-ssl.google.com/android/eclipse (if it didn’t work, the plugin can be downloaded and installed manually at http://developer.android.com/sdk/eclipse-adt.html ), then Developer Tools appears in the table below, check it and go on.
Writing your first Android app
After a successful installation, you can restart the Eclipse environment.

Installing the environment for Java: JDK and JRE

If you do not already have the Java Development Kit (JDK) and Java Runtime Environment (JRE) installed, you need to install them. You can do this at Oracle website Download and install the JDK and JRE.
Writing your first Android app

Installing Android SDK

The only thing left to do is to download and install the latest Android SDK. This is done at A site for Android developers I personally installed the SDK again in the honorable directory C:\Android After that you need to add platforms and other additional SDK elements. I added all available versions as I plan to make apps for earlier platforms as well as the USB driver and sample apps.
Writing your first Android app
Preparation for development is complete. Now the next step is to create the application.

Creating an Android app

Before you create your first app, you can create a virtual Android device to quickly test your newly written software on it. First I want to tell you a few words about the Android Virtual Device (AVD). This is a virtual Android Smartphone on which you can easily run the software you write. As you can see the advantage of the Android Virtual Device is that you can clearly see how your program will run on different Android smartphones, instead of buying the whole lineup and testing the application on each of them.
Let’s proceed to create a virtual Android device. In the menu, follow the path Window → Android SDK and AVD Manager
Writing your first Android app
Choosing New on the right side of the window, in the window that appears, enter the name of the virtual device, the platform (say Android 2.1), the memory card size (say 512 Mb), the type of display (say HVGA). Then click on the Create AVD button at the bottom.
Now let’s create a project. To do this, go to the menu path File → New → Other and in the list that appears. Android → Android Project
Writing your first Android app
As an example, I will tell you about the development of one of my simple programs UfaTermometr, which shows the current temperature from sensors located at one of the facilities of the local power company.
After creating the project, you will see a directory tree on the left. First of all, let’s upload your application icon, or rather 3 icons for different variants. In the folder drawable-hdpi upload png-picture with transparent background size of 72×72, in drawable-mdpi respectively 48×48 and drawable-ldpi the smallest size 36×36. You can do this by simply dragging and dropping files directly onto the tree. The next step is the controls. In my program I needed only three interface elements: ImageView(just a picture of the application logo), Button(to update the temperature value) and TextView(output of the temperature value). All these controls need to be described in a special xml-file. In the tree it is located at res → layout → main.xml Much of the layout of the controls is similar to the web page layout, it has padding, margin and a counterpart of align. The main.xmlcode of my application :
<? xml version =«1.0» encoding =«utf-8» ? >
< LinearLayout xmlns:android =" schemas.android.com/apk/res/android "
android:orientation ="vertical"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
< ImageView android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:src ="@drawable/droid"
android:layout_gravity ="center_horizontal|center"
android:layout_margin ="0px"
< Button
android:id ="@+id/refrbutton"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:gravity ="center_horizontal"
android:layout_gravity ="center_horizontal|center"
android:textSize ="30px"
android:padding =«20px»
android:layout_marginTop ="10dip"
android:text ="Update"
< TextView
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="…"
android:textSize ="100dip"
android:gravity ="center_horizontal"
android:id ="@+id/temper"
android:textColor ="#EEE3C4"
</ LinearLayout >
I won’t dwell on markup controls, because it’s all described in detail in the manual and generally intuitive to understand, especially for someone who has dealt with html/css-writing and/or development in any visual environment (such as Delphi). The only thing, note how I loaded an arbitrary image into ImageView. In the src field, you specify the path beginning with the @ symbol and then you specify the address along the tree. I created a drawable subfolder in the res folder ahead of time and "dropped" the logo in there and then just gave ImageView the path. Was it easy? Seems pretty easy to me.
The application code itself is located at the path src → "your package name" → "application name".java
By default, a "dummy" main class is created and you can then dance around it. I won’t go into details of such a simple code, but I will explain, that logic of work is explained by three simple steps :

  • Load HTML page with temperature values
  • Use regulars to "pull" the temperature
  • Show temperature value in TextView

Please note that when an application needs some system permissions, you need to list them in the manifest AndroidManifest.xml In this particular case we are using an internet connection, accordingly we add the line <uses-permission android:name="android.permission.INTERNET"/> to the above xml file.
The code itself, which implements the above logic of the :
package app.test.ufatermometr;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.*;
public class UfaTermometr extends Activity
public void onCreate(Bundle savedInstanceState)
final Button button = (Button) findViewById(R.id.refrbutton);
button.setOnClickListener( new Button.OnClickListener() {
public void onClick(View v) // click on the button
RefreshTemper(); // when starting, we load the temperature immediately
public String GetTemper(String urlsite) // temperature loading function
String matchtemper = "" ;
// page load
URL url = new URL(urlsite);
URLConnection conn = url.openConnection();
InputStreamReader rd = new InputStreamReader(conn.getInputStream());
StringBuilder allpage = new StringBuilder();
int n = 0;
char [] buffer = new char [40000];
while (n >= 0)
n = rd.read(buffer, 0, buffer.length);
if (n > 0)
allpage.append(buffer, 0, n);
// working with regulars
final Pattern pattern = Pattern.compile
( "<span style=\«color:#[a-zA-Z0-9]+\»> [^-+0]+([-+0-9]+)[^<]+</span> [^(a-yaa-yaeea-zA-Z0-9)]+([a-yaa-yaeea-zA-Z ]+)" );
Matcher matcher = pattern.matcher(allpage.toString());
if (matcher.find())
matchtemper = matcher.group(1);
return matchtemper;
catch (Exception e)
return matchtemper;
public void RefreshTemper()
final TextView tTemper = (TextView) findViewById(R.id.temper);
String bashtemp = "" ;
bashtemp = GetTemper( « be.bashkirenergo.ru/weather/ufa » );
tTemper.setText(bashtemp.concat( "°" )); // temperature display
After the application is written, you can already debug and test it.

Running and testing the application

Remember our virtual machine and run it with the usual button or menu bar Run → Run
This is the picture we can then contemplate :
Writing your first Android app
Now, if you want to share the app, you need to build the apk file. To do this, we’ll use the menu File → Export and in the list Android → Export Android application Next, select the project, then create a keystore and key storage, for this you will need to fill a few fields with all sorts of background information. The resulting apk file can be distributed and even posted to Android Market, but you will have to register and pay $25, which is not much, especially for a worthwhile project. But registration in the Market is probably a topic for a separate article.


In conclusion, I would like to say that undoubtedly Android app development is enjoyable, quite simple and interesting. Of course we only looked at the tip of the iceberg, but I hope that a light bulb went off in the minds of those of you who haven’t tried anything like this before and it’s quite possible that one day your app will be used by millions.

Sources, references

This post uses materials from :
APK app file

UPD:Remove possible errors

Defite :
1. error

ERROR: Unable to open class file C:\workspace\Test\gen\com\example\test\R.java: No such file or directory

solved by clearing project through the menu Project → Clean or by restarting Eclipse.
2. When an error occurs

emulator: ERROR: no search paths found in this AVD’s configuration. Weird, the AVD’s config.ini file is malformed. Try re-creating it

– This is a consequence of your username using Cyrillic characters. Remedy: go to Computer → System properties → Advanced system settings → Environment variables Create a new variable named ANDROID_SDK_HOME and the value is the path where the AVD folder is located (e.g. C:\Android\). Create, then find there path variable, open it and add path to Android SDK tools folder (for example, C:\Android\tools) separated by semicolons in the value field. Save it. Launch Eclipse, start the program.
UPD 05.10.2011, you can see what happened in the following screenshots:
Writing your first Android app
This is the result of the development of the very dummy given above as an example.
Designing and creating Android applications
Website creation