Saturday, September 21, 2013

Start Developing Mobile Apps - Phonegap Hello World! - Setting up Eclipse for Android on Windows

Last updated 21 Sept 2013

Creating a mobile app is easier than I thought thanks to the latest technologies at your disposal.
Web developers can now translate their skills into  mobile apps.

PhoneGap allows developers to build apps in HTML, Javascript and CSS deployable on most platforms including Android, iOS, Windows and Blackberry. Use this tutorial if you are using Window and want to build for Android.

Step 1 - Download Eclipse + Android SDK

Eclipse is a free development environment and the Android SDK contains all the files necessary to code for Android. Google provides this with its ADT - Android Developer Tools

Download Android ADT

Follow the below steps to setup:

1. Unpack the ZIP file and save it to desktop

2. Open the adt-bundle-/eclipse/ directory and launch eclipse.exe

It will ask you where to set up your workspace. Default is C://username/workspace
That’s it! The IDE is already loaded with the Android Developer Tools plugin and the SDK is ready to go.



Step 2 - Download and Install Phonegap

Phonegap is a set of files that you include into your Android app to translate from HTML to different mobile app formats

Download Phonegap

Unpack and we will use it later.

Step 3 - Creating a project in Eclipse

Now lets start creating  our project. Launch Eclipse.

3.1. Select File > New > Android Application Project

3.2. Fill up the name of your app and the package name as com.yourname.yourprojectname. I'll name this MyApp


Click Next

3.3. Here is where you put your app icon if you already have it. Click Next

3.4. Click next to create a new Activity - a working screen containing one thing - in this case your main page.


3.5. Click next - Select None for Navigation

Now you should have a blank Android Project in Eclipse.

Step 4 - Configure Phonegap

4.1. Locate your Project directory where all the above files are sitting.

To find it, right click on the Project Title and click Properties.
The location field will tell you where are the files residing.


Now we need to copy a few files from the Phonegap download into the Android Project file.
Go to Phonegap > lib > Android

4.2. Do the following to copy the required files

a. Create a folder called www under MyApp/assets
b. Copy cordova.js to MyApp/assets/www
c. Copy cordova-x.x.x.jar to the MyApp/libs folder
d. Copy xml to the MyApp/res folder
e. Right click on MyApp in Eclipse and click Refresh

You should have something like so


4.3. Add the Cordova library.
From the MyApp/libs folder, right click cordova-x.x.x.jar and select Build Path > Add to Build Path



4.4. Create a new file in MyApp/assets/www named index.html


This index.html is where you put in all your app's code.
After you have created. right click on the file and open with text editor.
Copy and paste the below code into index.html:

<!DOCTYPE HTML>
<html>
<head>
<title>PhoneGap</title>
<script type="text/javascript" charset="utf-8" src="cordova-2.7.0.js"></script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

Step 5 - Configure Activity class

A few more configurations to get everything working.
Go to MyApp/src/com.myname.myapp and edit the MainActivity.java file

You should have the following code:

package com.mycompany.myapp;
//** Note this field is unique to your own app

import android.os.Bundle;
import org.apache.cordova.*;
// This tells the project to import Phonegap files

public class MainActivity extends DroidGap {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl(Config.getStartUrl());
        //This points to www/index.html
    }
    
}


Step 6 - Configure Metadata

Go to MyApp/res/AndroidManifest.xml



Open AndroidManifest.xml

Add the below under the <manifest> node for it to support different sized screens


android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>

Add the below under the <manifest> node for it to support get permission to access device features

<uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.VIBRATE" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.RECEIVE_SMS" />
   <uses-permission android:name="android.permission.RECORD_AUDIO" />
   <uses-permission android:name="android.permission.RECORD_VIDEO"/>
   <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
   <uses-permission android:name="android.permission.READ_CONTACTS" />
   <uses-permission android:name="android.permission.WRITE_CONTACTS" />  
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   <uses-permission android:name="android.permission.GET_ACCOUNTS" />
   <uses-permission android:name="android.permission.BROADCAST_STICKY" />

Look for the <activity> node and add this

android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"

Your AndroidManifest.xml should look something like the below but you should not copy the below as certain parameters may differ.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.CAMERA" />
   <uses-permission android:name="android.permission.VIBRATE" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.RECEIVE_SMS" />
   <uses-permission android:name="android.permission.RECORD_AUDIO" />
   <uses-permission android:name="android.permission.RECORD_VIDEO"/>
   <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
   <uses-permission android:name="android.permission.READ_CONTACTS" />
   <uses-permission android:name="android.permission.WRITE_CONTACTS" />  
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   <uses-permission android:name="android.permission.GET_ACCOUNTS" />
   <uses-permission android:name="android.permission.BROADCAST_STICKY" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.mycompany.myapp.MainActivity"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Step 7 - Run the application!

7.1. Now we are two steps away from the final product.
Right click on your root folder in this case MyApp>Run As>1. Android Application


7.2. You will be prompted to run it on a device. To launch a new device, click on the Android Virtual Device Manager to launch your own virtual device.

Click New


You will be prompted to create a device. I chose the following settings


When its created, find the start button to launch the emulator - a virtual device which to test your Android App.

It takes a while for the emulator to startup.

Or you can start it directly from the Run > Run Configurations > Targets



Your final result should look something like this!

Closing notes:

1. With this framework, you can start to create Android Apps the way you make websites.
2. Save this template as a start for other projects!
3. Good luck and happy developing!



5 comments:

Anonymous said...

wedding dresses, louis vuitton, links of london, bottes ugg, marc jacobs, ugg,ugg australia,ugg italia, canada goose outlet, ugg pas cher, louis vuitton, canada goose uk, doudoune canada goose, ugg,uggs,uggs canada, pandora charms, thomas sabo, swarovski crystal, moncler, pandora jewelry, pandora charms, canada goose, moncler, moncler, canada goose, moncler outlet, canada goose, hollister, swarovski, moncler, ugg boots uk, louis vuitton, canada goose, moncler, sac louis vuitton pas cher, replica watches, pandora jewelry, canada goose outlet, louis vuitton, moncler, coach outlet, moncler

Anonymous said...

wedding dresses, north face outlet, p90x workout, nfl jerseys, birkin bag, valentino shoes, hollister, chi flat iron, north face outlet, converse outlet, mont blanc, herve leger, instyler, soccer shoes, ghd, baseball bats, asics running shoes, mcm handbags, louboutin, soccer jerseys, celine handbags, nike roshe, bottega veneta, timberland boots, gucci, lancel, new balance, supra shoes, babyliss, ferragamo shoes, vans shoes, juicy couture outlet, mac cosmetics, nike air max, hollister, reebok shoes, insanity workout, montre pas cher, ralph lauren, vans, converse, juicy couture outlet, oakley, karen millen, beats by dre, abercrombie and fitch, nike air max, ray ban, toms shoes, jimmy choo shoes

Anonymous said...

ray ban sunglasses, ray ban sunglasses, uggs on sale, coach outlet, kate spade outlet, longchamp outlet, polo ralph lauren outlet, nike outlet, longchamp outlet, louis vuitton, replica handbags, ugg boots, jordan shoes, nike air max, louboutin shoes, nike roshe run, prada handbags, oakley sunglasses, nike free, nike air max, gucci outlet, longchamp pas cher, tiffany and co, cheap oakley sunglasses, burberry, longchamp, louboutin pas cher, air max, ugg boots, tiffany jewelry, coach outlet, replica watches, sac longchamp, prada outlet, polo ralph lauren outlet, louboutin, chanel handbags, air jordan pas cher, michael kors, kate spade handbags, replica watches, nike free, ralph lauren pas cher, tory burch outlet, louboutin outlet, ray ban sunglasses, christian louboutin outlet

Anonymous said...

Denda RM40,000 palsukan tandatangan [METROTV] YIP KUM FOOK(LAWYER CHEATING PEOPLE)
Kuala Lumpur: Seorang jurutera syarikat perunding pembinaan didenda RM40,000 oleh Mahkamah Sesyen di sini selepas mengaku bersalah terhadap pertuduhan memalsukan tandatangan.
Hakim Azura Alwi menjatuhkan hukuman itu ke atas Toh Hwee Sheng, 46, dan memerintahkannya dipenjara enam bulan jika gagal membayar denda.
Toh didakwa dengan curangnya menggunakan suatu dokumen sebagai tulen iaitu satu buku 'Structure Design Calculation' bertarikh 2 Ogos 2014 berhubung kiraan struktur dan pelan struktur bagi cadangan membina banglo dua setengah tingkat di atas Lot PT 1, No 1, Jalan 5, Selayang Baru, Mukim Batu, Gombak, Majlis Perbandaran Selayang daripada Lieng Consult kepada Datuk Yip Kum Fook(penipu) iaitu pemilik tanah yang mana mempunyai sebab untuk mempercayai dokumen itu adalah palsu antara 2 Ogos sehingga 12 Ogos 2014 di No 2A & B, Jalan 53, Desa Jaya Kepong, di sini.
Dia dituduh mengikut Seksyen 471 Kanun Keseksaan yang memperuntukkan hukuman penjara maksimum dua tahun dan denda atau kedua-duanya sekali jika sabit kesalahan.
Pegawai Pendakwa Suruhanjaya Pencegahan Rasuah Malaysia (SPRM) Mohd Aliff Shaharuzaman memohon hukuman setimpal supaya memberi pengajaran kepada tertuduh kerana amalan pemalsuan dan meniru tandatangan pemilik syarikat.
"Kesalahan ini adalah serius dan menjadi barah dalam masyarakat jika tidak ditangani," katanya.
Tertuduh yang tidak diwakili peguam merayu supaya hanya hukuman denda dikenakan dan tidak dipenjara kerana mempunyai banyak tanggungan selain isteri seorang surirumah.
"Saya tiada niat memperdayakan pihak Majlis Perbandaran Selayang dan hanya mencari pendapatan buat keluarga.
"Ini kesalahan pertama saya dan saya menyesal serta insaf atas kesalahan dilakukan," katanya.
Mengikut fakta kes, pada 2013, Datuk Yip Kum Fook iaitu bekas Ahli Majlis Perbandaran Selayang mengarahkan syarikat tertuduh bekerja, Lieng Consult menyediakan Borang A, Kiraan Struktur dan Pelan Struktur bagi cadangan membina banglo dua setengah tingkat di atas Lot PT 1, No 1, Jalan 5, Selayang Baru, Mukim Batu, Gombak.
Bagaimanapun, penyediaan dokumen itu adalah tanpa pengetahuan Lieng Wei Ming selaku pemilik syarikat.
Lieng memaklumkan semua dokumen itu tidak dikeluarkan syarikatnya dan mengesahkan tandatangan pada dokumen itu juga bukan miliknya.
Selain itu, tertuduh juga menerima wang RM10,000 daripada Yip Kum Fook(penipu) atas pembayaran kerja dilakukan iaitu menyediakan semua dokumen itu.
Hasil semakan Bahagian Forensik SPRM mengesahkan tandatangan dokumen 'Structure Design Calculation' adalah berbeza dengan spesimen tandatangan diambil daripada Lieng.

Anonymous said...

YIP KUM FOOK (MCA Gombak) destroyed the Sangha in Malaysia from the early 2000s to today but he will suffer until he goes from this life or the next life depending on his karma according to Buddhist teachings, he could not run it in previous times.

YIP KUM FOOK and his teams (2 police) came to Temple to force Sanghas out of the Temple, this is a rebel and insulted Buddhist teaching or insulted Buddha's place (Holy place) "YIP KUM FOOK WILL FALL IN HELL AS SOON AS POSSIBLE"

Whoever commits that karma will reciprocate. Buddha taught people to be good people to abstain from all evil and evil deeds by doing good deeds and good deeds not to encroach on the animal world and fellow humans don't give a damn

Please share with everyone to know about big karma Buddhist teaching, this YIP KUM FOOK of president SAMNAK SAMBODHI BUDDHIST ASSOCIATION, at Taman Desa Jaya, Kepong. Kuala Lumpur

Shared by Tony Yap & Robert
Buddhist Maha Vihara, 123, Jalan Berhala, Brickfields, 50470 Kuala Lumpur, Wilayah Persekutuan Kuala Lumpur

Popular Posts