Write your first app on Android

Preface
The purpose of this post — on the one hand to share its successful experience of start developing applications on the Android platform and on the other hand to contribute to the development of the market of software for this wonderful and rapidly growing platform for the account (without false modesty I can say) maybe You read this post. In the network, of course, you can find materials on the topic of application development "is a little more complicated than helloworld", but as a rule they are scattered and do not describe the various small underwater rocks. In this post we will consider the full cycle of application development, starting with a clean computer to the finished apk file. Under the cut screens.
Preparing for development
In order to prepare for the development does not require any special handling and manipulation. I will describe step by step what to do. I have Windows XP, so all the below should be attributed to the OS, although without any strong changes can be applied to other Windows OS and even Linux.
-
the
- setting up the Eclipse development environment the
- Install the ADT plugin for Eclipse the
- install the JDK and JRE the
- Install Android SDK
setting up the Eclipse development environment
Everything is simple. Go to the Downloads section on official website and download version Classic. Then just unzip the archive wherever you want, I decided to do it in an honourable directory C:\Eclipse
Installing the ADT plugin for Eclipse
Launch Eclipse and open the install dialog of the plug-in menu Help → Install new software. In the field Work with enter the load address of the plugin dl-ssl.google.com/android/eclipse (if not, then the plugin can be downloaded and installed manually the link http://developer.android.com/sdk/eclipse-adt.html)hereinafter, the following table will appear Developer Tools, mark it and go on.

After successful installation, you can restart the Eclipse.
setting the environment for Java: JDK and JRE
If you have not installed Java Development Kit (JDK) and Java Runtime Environment (JRE), you must install them. This can be done on Oracle. Download and install JDK and JRE.

Install Android SDK
The case remained for small — to download and install the latest Android SDK. This is done on Android developers. I personally installed SDK again in honor directory C:\Android. After this you need to add the platform and other additional elements of the SDK. I added all available versions, as I plan to do the application and for the early platforms, as well as the USB driver and application examples.

The preparation for development completed. Now the next step is creating apps.
Create Android app
Before you create your first application, you can create a virtual Android device to quickly test your svezhenanesenny software. First I want to say a few words about Android Virtual Device (AVD). This is a virtual Android device on which you will easily be able to run your program. As you can see, the advantage of the Android Virtual Device is that clearly show what will run Your program on different smartphones with Android, instead of buying the whole range and test the application on each of them.
Begin by creating a virtual Android device. Follow the menu path Window > Android SDK and AVD Manager.

Select New in the right part of the window, in the window that appears, enter the name of the virtual device platform (say Android 2.1), the size of the memory card (say 512 Mb), display type (say HVGA). Then click below on the button Create AVD.
Now create the project. To do this, go along the path in menu File → New → Other, the list Android → Android Project.

As an example, talk about the development of one of my simplest programs UfaTermometr showing the current temperature from sensors located on one of the objects of the local energy company.
After creating the project, on the left you will see a tree of directories. First things first, upload your app icon, but rather the 3 icons under different options. In the folder drawable-hdpi ship png-image with transparent background in size 72x72, drawable-mdpi, respectively 48x48 and in drawable-ldpi smallest size 36x36. This can be done by simply dragging files directly into a tree. The next step will be controls. In my program, it was necessary only three elements of the interface: ImageView (just a picture of the app logo), Button (to update the temperature value) and a TextView (displaying temperature). All these controls should be described in a special xml file. In the tree it is located at res → layout → main.xml. In many respects the layout of the controls is similar to the layout of the web pages, there is padding, margin, etc align. Code main.xml my app:
<?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:parent="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>
Elaborate on the layout of the controls won't as it's pretty detailed in the manual and generally intuitive, especially to those who have dealt with html/css layout and/or design in any visual environment (e.g., Delphi). The only note I have uploaded a custom image in the ImageView. In the field src specifies the path, starting with the @ symbol and then indicate the address of the tree. In the res folder I had previously created a sub-folder drawable and "threw" in there logo, and then just have the ImageView is the way to go. Easy? For me so much.
The code itself is on the way src → "your package name" → "application name".java.
the
Actually the code that implements the above logic:
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.An InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.*;
public class UfaTermometr extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R. layout.main);
final Button button = (Button) findViewById(R. id.refrbutton);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) // click on the button
{
RefreshTemper();
}
});
RefreshTemper(); // at startup the temperature of the ship immediately
};
//----------------------------------------------------------------
public String GetTemper(String urlsite) // function load temperature
{
Matchtemper String = "";
try
{
// load page
URL URL = new URL(urlsite);
URLConnection conn = url.openConnection();
An InputStreamReader rd = new an 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);
}
}
// work with regexps
final Pattern pattern = Pattern.compile
("<span style=\"color:#[a-zA-Z0-9]+\">[^-+0]+([-+0-9]+)[^<]+</span>[^(a-AA-Aeea-zA-Z0-9)]+([a-AA-Aeea-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()
{
tTemper final TextView = (TextView) findViewById(R. id.temper);
Bashtemp String = "";
bashtemp = GetTemper("be.bashkirenergo.ru/weather/ufa");
tTemper.setText(bashtemp.concat("°")); // display temperature
};
}
Once the application is written, you can already debug, and test.
Run and test the application
Remember about our virtual machine and run the usual button or menu Run → Run
This is the picture we can then contemplate:

Now, if you want to share an app, you need to build an apk-file. To do this, use menu File → Export and list Android → Export Android application. Next, select the project, then create the keystore keystore and key, you will need to fill in a few fields with all kinds of reference information. The resulting. apk file you can distribute and upload to the Android Market, but you have to register and pay $25, which in General is a bit, especially for a worthwhile project. But the registration in the Market, perhaps a topic for another article.
Opinion
In conclusion, I would say that certainly the development of applications for Android enjoyable, quite simple and interesting. Of course, we have considered only the tip of the iceberg, but I hope that in the minds of those of you who have never tried to do anything like "turn on light" and it is possible that your application will benefit millions.
links
The post materials used:
www.ibm.com
www.itblog.name
Wikipedia
APK apps
UPD: Troubleshooting potential errors
Defite:
1. Error
ERROR: Unable to open class file C:\workspace\Test\gen\com\example\test\R.java: No such file or directorysolved cleaning the project via the menu Project → Clean or restart 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 itis the consequence of the fact that You have Cyrillic in user name. Solved: go to the Computer → system Properties → advanced system settings → environment Variables. Create a new variable called ANDROID_SDK_HOME and value – by where the AVD folder (for example, C:\Android\). Create, next, look for the same Path variable, open and in the value field, using semicolons add the path to the tools folder of the Android SDK (for example, C:\Android\tools). Persistent. Run Eclipse, run the program.
UPD 05.10.2011 what has become of this application is you can see on the following screenshots:

It is the result of the development of the blanks, which is above given as example.
Develop and build Android applications
Creating websites
Комментарии
Отправить комментарий