Anda di halaman 1dari 35

ANDROID

Persiapan Pemograman Android


Untuk Membuat program berbasis android diperlukan beberapa persiapan yaitu : Install software Eclipse yang basisnya menggunakan bahasa pemrograman Java. Software ini GRATIS! Dan sudah dulengkapi berbagai macam plugin untuk mengembangkan aplikasi berbasis Android. Perlu diketahui, untuk menjalankan Eclipse, kita tidak perlu melakukan instalasi terlebih dahulu. Dan jika Anda tidak memiliki HP Android, Jangan khawatir, karena dengan Eclipse kita sudah dibekali emulatornya. Terlebih dahulu harus Anda memiliki 3 buah komponen dibawah ini. Anda bisa mengunduhnya dengan cara meng-klik tulis dibawah ini. 1. Eclipse (klik untuk mendownload) 2. ADT Plugin (klik untuk mendownload) 3. SDK (klik untuk mendownload) Selanjutnya ikuti langkah demi langkah berikut. Semoga berhasil!

MENGINSTAL PLUGIN ADT

Cara 1. Menginstal ADT Plugin untuk eclipse langsung dari server. Pada cara pertama, Anda harus terhubung dengan internet. 1. Jalankan Eclipse, kemudian pilih help > instal new software. (Gambar 1.1) 2. Klik add di sebelah kanan atas. 3. Pada kotak dialog add repository, isikan nama : ADT dan location : https://dlssl.google.com/android/eclipse/. (Gambar 1.2) 4. Lihat kotak Available software. Centang pada item Developer Tools kemudian pilih Next. (Gambar 1.3) 5. Pada tampilan berikutnya, Anda akan melihat daftar tools yang akan di download lebih detail. Pilih Next.

6. Anda akan melihat license agreement. Pilih Accept, kemudian klik Finish. 7. Setelah proses instalasi selesai, restart eclipse. Cara 2. Apabila Anda terkendala dengan koneksi internet, Anda juga bisa melakukan instalasi plugin secara offline. Caranya, download terlebih dahulu ADT Plugin kemudian simpan di direktori tertentu. 1. Ikuti langak 1 dan 2 pada cara pertama 2. Pada dialog add site, pilih Archive. 3. Cari dan pilih file adt.zip yang Anda download tadi. 4. Jangan lupa memberi nama pada beris nama. (Gambar 1. 4)

5. Klik ok. 6. Lihat kotak Available software. Centang pada item Developer Tools kemudian pilih Next. (Gambar 1.5)

7. Pada tampilan berikutnya, anda akan melihat daftar Tools yang akan di instal. Pilih Next. 8. Anda akan melihat license agreement. Pilih Accept, kemudian klik Finish. 9. Setelah proses instalasi selesai, restart eclipse. DOWNLOAD PACKAGE SDK Pada tahap ini, Kamu harus terhubung dengan internet untuk melakukan update repositori. Jika tidak, maka Kamu tidak punya package Android. Berikut caranya : 1. Pada Eclipse, pilih Window > Android SDK and AVD manager (Gambar 1.6)

2. Pilih Available Package (Gambar 1.7) 3. Centang pada Android Repository (Gambar 1.8) 4. Otomatis akan melakukan Update , jika tidak pilih Refresh

5. Centang Android Repositori, kemudian pilih Instal Selected

MEMBUAT ANDROID VIRTUAL DEVICE (AVD) Nah, saatnya membuat Android Virtual Device (AVD). Ini nanti yang akan dipanggil sebagai simulator. 1. Pada kotak dialog Android SDKdan AVD Manager, pilih New (Gambar 1.11) 2. Isikan seperti berikut (Gambar 1.12) Name AVDku Target Android 2.1 ( atau yang lain ) SDCard Dibiarkan saja Snapshot Dibiarkan saja Skin WQVGA400 Hardware Dibiarkan saja

Nah, dengan demikian, sekarang Anda sudah memiliki Emulator Android. :D Menyenangkan bukan?? Mau tahu seperti apa emulatornya??

KONFIGURASI PLUGIN ADT 1. Jalankan Eclipse, pilih window > Preference 2. Pada panel sebelah kiri, pilih Android

3. Pada SDK Location, klik Browse, cari kemudian pilih SDK File yang sudah anda miliki sebelumya 4. Pilih salah satu platform android, kmeudian klik Apply . 5. Klik Ok.

ANDROID
Membuat program Android Sederhana

Membuat program Android Sederhana Melanjutkan tulisan sebelumnya maka kali ini kita akan membuat program Android sederhana. Step by Stepnya adalah sebagai berikut: Buka IDE Eclipse kita. Kemudian buat project baru melalui menu File > New > Other (Android Project) > OK. Pada jendela New Project, isikan Project Name, misal Konversi Rupiah; Tentukan target mesinnya, misal Android 2.2; Isi Application Name, misal Konversi Rupiah. Isi Package Name, misal konversi.rupiah. Isi Create Activity, misal Convert. Isi SDK Version, misal 8. Klik Finish. Setelah membuat project, maka secara otomatis akan ter-generate beberapa folder dan file, Masuk folder res > values, edit file strings.xml (double click), Pada bagian ini kita akan menambahkan property komponen yang akan kita gunakan. Pada jendela Android Resources, tambahkan beberapa item, sbb (catatan, Anda dapat

langsung mengedit file strings.xml, atau melalui antar muka grafis. <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Konversi Mata Uang Rupiah</string> <string name="app_name">Konversi Rupiah</string> <color name="myColor">#A3C65B</color> <string name="buttonHandler">myClickHandler</string> <string name="usdollar">US Dollar</string> <string name="jpnyen">Japan Yen</string> <string name="ausdollar">AU Dollar</string> <string name="calc">Kalkulasi</string> </resources> Masuk folder res > layout, editlah file main.xml (double click), Pada bagian ini kita akan menambahkan komponen yang akan membentuk layout aplikasi (Tambahkan sebuah EditText, sebuah RadioGroup, tiga buah RadioButton, dan sebuah Button. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/myColor"> <EditText android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:inputType="numberSigned|numberDecimal"></EditText> <RadioGroup android:id="@+id/RadioGroup01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:id="@+id/RadioButton01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/usdollar" android:checked="true"></RadioButton> <RadioButton android:id="@+id/RadioButton02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/jpnyen"></RadioButton> <RadioButton android:id="@+id/RadioButton02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/ausdollar"></RadioButton> </RadioGroup> <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/calc" android:onClick="@string/buttonHandler"></Button> </LinearLayout> Setelah membuat menentukan property dan layout komponen, maka langkah selanjutnya

adalah menambahkan logika pemrograman pada file Convert.java;

package konversi.rupiah; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.RadioButton; import android.widget.Toast; public class Convert extends Activity { private EditText text; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text = (EditText) findViewById(R.id.EditText01); } public void myClickHandler(View view) { switch (view.getId()) { case R.id.Button01: RadioButton usdollarButton = (RadioButton) findViewById(R.id.RadioButton01); RadioButton jpnyenButton = (RadioButton) findViewById(R.id.RadioButton02); RadioButton ausdollarButton = (RadioButton) findViewById(R.id.RadioButton03); if (text.getText().length() == 0) { Toast.makeText(this,"Silahkan isi Angka yang benar!", Toast.LENGTH_LONG).show(); return; } float inputValue = Float.parseFloat(text.getText().toString()); if (usdollarButton.isChecked()) { text.setText(String.valueOf(convertRupiahToUSD(inputValue))); } else if (jpnyenButton.isChecked()){ text.setText(String.valueOf(convertToJPY(inputValue))); } else if (ausdollarButton.isChecked()) { text.setText(String.valueOf(convertToAUD(inputValue))); } } } // Konversi ke USD private float convertRupiahToUSD(float usd) {

return (usd * 9000); } // Konversi ke JPY private float convertToJPY(float jpy) { return (jpy * 110); } // Konversi ke AUD private float convertToAUD(float aud) { return (aud * 8300); } } Setelah menambahkan logika pemrograman pada file Convert.java, maka langkah terakhir adalah menjalankan aplikasi Run > Android Application. Android Sample Project Catatan: Sebelum me-Run program, pastikan Anda telah membuat Android Virtual Devices (AVD) terlebih dahulu. Selamat mencoba.

How to Export and Import Android Virtual Devices


Published August 24, 2011 Android 16 Comments Tags: android, avd, emulator, tutorial

Over the last couple of weeks, I have been testing my Android app on different devices. That work is going well. In this note, Id like to share a couple of things that came out as side effects of that work, including:
1. How to move Android virtual device (AVD) files from an Android Emulator to another machine. 2. The set of AVD definitions that I found useful in checking an app on different screen sizes and screen densities. Knowing how to move AVD files is important for a couple of reasons: (a) if you ever switch machines or do a complete reinstall of your Android environment, it would be good if you could reuse old device definitions rather than having to recreate them; (b) If you are working on a team, it saves a lot of time if there is a shared set of devices that everyone in the team is testing against. How To Move AVD Files

There are a few things you need to understand about Android virtual device (AVD) files. The first is what they are. Basically, they are files that hold the description of an Android device and whats on that device in the Emulator environment. If you need more information, start with the Android Developers note on Managing Virtual Devices. Whether you are working alone or on a team, sooner or later you will want to know how to move your AVD files from one machine to another. Heres what I have learned about that task.
Where do AVD definitions reside on your disk?

To find your avd folder on you machine, check your user directory. For me, with user name blahti, those locations are:

Windows 7: \users\blahti\.android Windows XP: C:\Documents and Settings\blahti\.android\ on Windows XP Linux/Mac: ~/.android

Heres a tip for new Mac users (like me). In the Finder window, you have to use the Go To Folder item on the Go menu to get to ~/.android.
Steps for moving

Here are the basic steps:


1. Go to the .android folder and pick the avd you want to export.

2. Compress the device.avd folder and the device.ini file. (Replace device with the name of the device you want to copy. For example: Evo4g.avd, Evo4g.ini.) 3. Copy the compressed files to the new location, usually on a different machine. 4. Extract the files and place them in the users .android folder. 5. Edit the ini file and make corrections, as needed: (a) user name could be different; (b) change slash in path to Windows or Unix slash.

Example ini file contents on a Mac:


target=android-8 path=/Users/bill/.android/avd/Evo4G.avd

Example ini file contents on Windows 7:


target=android-8

path=C:\Users\blahti\.android\avd\Evo4G.avd Example ini file contents on Windows XP:


target=android-8 path=C:\Documents and Settings\blahti\.android\avd\Evo4G.avd

Start the AVD Manager under Eclipse and be sure the files are readable. See Figure 1 below.

Figure 1 Start the AVD Manager in Eclipse If there is an error, a red X is displayed, as in Figure 2. Highlight the item, and see if the Repair button is enabled. Click Repair if it is. Or click the Details button for more

information. In most cases, it will be something wrong with the path in the ini file you just edited. .Always check that first. After you edit a file, click the Refresh button to see if your correction worked. Figure 3 shows that all corrections have been accepted.

Figure 2 AVD Manager shows errors

Figure 3 AVD Manager shows corrections To use the new AVD definitions, run the ADB Device Manager. You will need two pieces of information: (a) the diagonal measure for your computers screen; (b) the actual size of the screen for the Android phone or device. Click Start and fill in the diagonal screen size.

Figure 4 Start a Device from the AVD Manager Here are a few values for diagonal size (the d value). If you are testing with other devices, a Google search for your phone name with the word specs usually gets you what you want. HTC Evo 480 x 800, LCD 217, d 4.3 Motorola Droid 3 540 x 960, LCD 275, d 4 Motorola Droid 480 x 854, LCD 265, d 3.7 NexusOne 480 x 800, LCD 252, d 3.7 LG Optimus S 320 x 480, LCD 180, d 3.2 HTC Tattoo, 240 x 320, LCD 143, 2.8 small screen, low density
My Set of Android Virtual Devices For Testing

Here is the set of virtual device definitions that I have been using in my testing. The set includes large and small screen sizes and low, medium, and high density screens. I have not tried tablets yet.

To get the zip files for these AVD definitions, go to my shared avd folder in Google Docs. When you get there, do not click on the zip files there. Instead right-click with the mouse button (control-click on a Mac). That gets you to a menu with a Download link on it. To use them, just download the zip file and move the files to the right place, as described in the first section of this note. Each of these comes with a Dashboard UI Demo app already installed. To give you an idea of what these different devices look like, heres a screenshot of the six devices running a demo program. Basically, it looks reasonable except on the small screen of the HTC Tattoo.

Figure 5 Demo program running on different virtual devices The AVD files encode the device information shown in the previous section. If you add more device definitions and have to calculate the LCD value, here is the formula: LCD = sqrt (x^2 + y^2) / d

References

Id like to thank once again the Android community, and particularly the people who support the Android Developers website, for their willingness to share information and help one another. Here are the references I found most useful for this work: Managing Virtual Devices an intro article on the Android Developers website. It describes what an AVD file is, what it contains, and how its used in the Android Emulator. Supporting Multiple Screens - a good place to start to understand how to deal with the different screen sizes and densities. Once you start testing on multiple devices, youre likely to discover that you have problems. This article and the next one are essential references. Providing Resources - an article on how you structure your application to handle different devices. Question at StackOverflow about testing on multiple devices I liked the suggestions about which set of devices to choose for a representative set, given the current statistics on actual devices in use. Screen Sizes and Densities current data on what devices are in use. This was mentioned in the Stack Overflow note above.

Android development tools installation


Lars Vogel

Version 8.4 Copyright 2009, 2010, 2011, 2012, 2013 Lars Vogel 04.08.2013 How to install the Android development tools This document describes how to install and configure the Android development tools.

Table of Contents
1. Installation of the Android development tools

2. Requirements 2.1. System requirements 2.2. Requirements for using a 64bit Linux 3. Install Android Developer Tools 3.1. Download packaged Android Developer Tools 3.2. Stand-alone ADT installation 3.3. Update an existing Eclipse IDE 4. Install Android SDK version 4.1. Using the Android SDK manager 4.2. Install support library 5. Accessing the Android source code in Eclipse 5.1. Installation 5.2. Connect source to your project 5.3. Validate 6. Exercise: Create and start Android Virtual Device 6.1. Target 6.2. Create AVD 6.3. Start your AVD 7. Android Studio 7.1. Android Studio based on IntelliJ IDEA 7.2. Installation 8. Updating an Eclipse IDE for Android development 8.1. Install ADT Plug-ins and Android SDK 8.2. Manual installation of the Android SDK

1. Installation of the Android development tools

Google provides tools to develop Android applications. You can choose between Eclipse or IntelliJ based tools. This guide describes both approaches but the focus is on the Eclipse based tooling.
Tip

Selecting an IDE is primarily based on personal preferences. The team at Google plans to support both the Eclipse as well as the IntelliJ based tools. The author of this text is a long term Eclipse user, contributor and committer and therefore selects Eclipse as his primary tooling for Android development.

2. Requirements
2.1. System requirements

Development for Android can be done on a reasonably sized computer. For a nice experience a modern computer is recommended, for example a 2.6 GHz CPU with at least 8 GB of memory. An SSD speeds up the start of the Android emulator.
2.2. Requirements for using a 64bit Linux

The Android SDK is 32bit, therefore on a 64bit Linux system you need to have the package ia32-libs installed. For Ubuntu you can do this via the following command.
apt-get install ia32-libs

Please check your distribution documentation, if you are using a different flavor of Linux.

3. Install Android Developer Tools


3.1. Download packaged Android Developer Tools

Google provides a packaged and configured Android development environment based on the Eclipse IDE called Android Developer Tools. Under the following URL you find an archive file which includes all required tools for Android development: Getting the Android SDK.
3.2. Stand-alone ADT installation

Extract the zip file and start the Android Developer Tools (Eclipse) which are located in the eclipse folder. You can do this by double-clicking on the eclipse native launcher (e.g., eclipse.exe under Windows).

3.3. Update an existing Eclipse IDE

See Section 8, Updating an Eclipse IDE for Android development for a description on how to update your existing Eclipse IDE to perform Android development.

4. Install Android SDK version


4.1. Using the Android SDK manager

The Android SDK Manager allows you to install specific versions of the Android API. Select Window Android SDK Manager from the Eclipse menu or use the tools/android program from your Android SDK installation.

Tip

If you used the packaged Android Developer Tools download based on the Eclipse IDE, the Android SDK is located in the sdk folder of the extracted archive file. The Android SDK Manager allows you to install and delete Android packages. Select from the tree the version of Android you would like to develop for and press the Install button. The following screenshot shows the selection for the API 16 version of Android.

Press the Install button and accept the license for all packages. After the installation is completed, restart the Eclipse IDE.

4.2. Install support library

In the Android SDK Manager select Extras and install the Android support library. Android currently has several versions of the library, the v4, v7 and v13 version which are valid as of the respective API level of Android. The usage of a higher version of the support library is typically based on the lower version. For example the support for the action bar for Android devices with version API7 is included in the support library v7. This library requires also the v4 library.

5. Accessing the Android source code in Eclipse


5.1. Installation

During Android development it is useful to have access to the Android source code. You can download the Android source code via the Android SDK Manager by selecting the Sources for Android SDK.

The sources are stored in the path_to_android_sdk/sources/android-xx directory. . xx is the API level of Android, e.g. 19 for the Android 4.4 version.
5.2. Connect source to your project

To connect the sources with the android.jar file in your Android project, right-click on your android.jar in the Package Explorer view and select Properties Java Source Attachment. Select External location and press the External Folder button. Browser to the path_to_android_sdk/sources/android-xx location and press the OK button.

5.3. Validate

Validate that you can see the Android source code. For example open the View class via the Open Type dialog (Ctrl+T) and ensure that you see the source code.

6. Exercise: Create and start Android Virtual Device


6.1. Target

In this exercise you create and start an AVD. Even if you have a real Android device available, you should get familiar with the creation and usage of AVDs. Virtual devices give you the possibility to test your application for selected Android versions and a specific configuration.
6.2. Create AVD

Define a new Android Virtual Device (AVD) by opening the AVD Manager via Window Android Virtual Device Manager and by pressing the New button.

Enter values similar to the following screenshot.

Tip

Ensure that the Use Host GPU option is selected. This allows the AVD use the graphical processing unit of your computer and makes rendering much faster. Afterwards press the OK button. This will create the AVD configuration and display it under the list of available virtual devices.
6.3. Start your AVD

Select your new entry and press the Start button. Select Launch in the following dialog.

Warning

Do not interrupt this startup process, as this might corrupt the AVD. The first start may take up to 10 minutes on an older machine. On a modern machine it typically takes 1-3 minutes for a new AVD to start. After the AVD started, you can control the GUI with the mouse. The emulator also provides access to the phone buttons via a menu on the right side of the emulator.

Tip

Once started, don't stop the AVD during your development. If you change your application and want to test a new version, you simply re-deploy your application on the AVD.

7. Android Studio

7.1. Android Studio based on IntelliJ IDEA

Google also provides a modified version of the IntelliJ IDE called Android Studio for developing Android applications.
7.2. Installation

This tutorial uses Eclipse for its description but very similar functionality is available for Android Studio. Use Installing Android Studio to learn how to install and use it.

8. Updating an Eclipse IDE for Android development


8.1. Install ADT Plug-ins and Android SDK

The following description assumes that you have already a flavor of the Eclipse IDE installed which you want to update to develop Android applications. Use the Eclipse update manager via Help Install new software and start the android to install all available components for the Android Development Tools (ADT) from the following URL:
https://dl-ssl.google.com/android/eclipse/

After the new Android development components are installed, you will be prompted to install the Android SDK. You can use the following wizard or go to the next section to learn how to do it manually.

8.2. Manual installation of the Android SDK

After the installation of the ADT the Eclipse tooling allows to download the Android SDK automatically. Alternatively you can also manually download the Android SDK from the Android SDK download page.
http://developer.android.com/sdk/index.html

The download contains a zip file, which you can extract to any place in your file system, e.g. on my Linux system I placed it into the /home/vogella/android-sdks folder. Avoid using spaces in the path name, otherwise you may experience problems with the usage of the Android SDK. You also have to define the location of the Android SDK in the Eclipse Preferences. In Eclipse open the Preferences dialog via the menu Window Preferences. Select Android and enter the installation path of the Android SDK.