Thursday, September 2, 2010

Android Development Tutorial/Overview - with sample code.

Android Development/Programming:
The basic concept of android is JFX(or JSP) like development of UI in java. You should know that android testing using emulator is very slow (unless you do it on an actual device eg droid incredible)- and unlike iphone you can freely deploy your apps on your phone. Android (like java permission file or vista/widows 7 model) like to warn before installing application - and applications have to indicate what kind of permission it needs.


Prerequisites:
Before starting Android development - you should know java first - by reading books like Core java (other programming back ground is useful) etc


Getting Started:
You should get started using :

http://www.vogella.de/articles/Android/article.html (or androids website).

Other links:

With android - you have to develop java program like in 90s(but at least its still java) :
http://developer.android.com/guide/practices/design/performance.html

You can access unix shell (eg adb shell) and analyze memory from this tutorial:
http://kohlerm.blogspot.com/2009/04/analyzing-memory-usage-off-your-android.html


 

Open GL ES

Android uses Open GL ES(subset of Open Gl for mobile ) for game development- just like Iphone.
 Its however tough to find good books or a good tutorial on Open Gl Es for android.

Android Game video tutorial:
http://www.edu4java.com/androidgame.html

Android 3D game tutorial

http://www.droidnova.com/android-3d-game-tutorial-part-i,312.html

Making games on Android
http://www.brighthub.com/hubfolio/matthew-casperson/blog/archive/2010/01/04/making-games-on-android.aspx

Getting started Link -in games - http://www.brighthub.com/hubfolio/matthew-casperson/blog/archive/2010/01/04/making-games-on-android.aspx

OpenGlEs for Iphone(similar to OpenGl Es on Android)
http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-table-of.html

Opengl(not openGles) redbook
http://fly.cc.fer.hr/~unreal/theredbook/

 

 
Android Books :

a) General Ones: 
This a tough one(as they are moderate quality or will easily get outdated).
However top books seems to be :
Professional Android 2 application development (This one covers most topics-  but does not explain in detail)

Pro Android 2 ( This one is similar to above book.. But I dont like reading on the cheap - yellow paperback pages provided by apress . This book was hard for me to get online - but i could preview it at barnes and nobles store - and it seemed verbose to me. )

The above two books covers the most topics - and may be your best bet now(yet- june 2010).. However there style is not good(verbose, making book huge with code/examples without clearly explaining them)
(The Unlocking Android seemed promising to me - but was outdated.  The Hello, Android may be promising. I however liked the tutorial style(not content) of the heavily criticized book - Android Application Development: Programming with the Google SDK )
 
I have however chosen to download pdf books from http://www.commonsware.com/ which provided good content for the price - but its not a replacement for having physical book in your hand.(This site however did not post to https by default- and had no facility to change password)

b) Game Books:
There are insufficient material on developing games. But here you go:
Pro Android Games  (its useful - only if you already know open gl and c++)
Beginning Android Games (not yet released)

You may need to buy books to brush your maths (but first try some games and see if you get around by googling)
 3D Math Primer for Graphics and Game Development (Wordware Game Math Library)

Mathematics for 3D Game Programming & Computer Graphics (Game Development Series (Charles River Media).)



Running google Samples:
Once you have got your eclipse setup . The next step is to run google samples(that you downloaded with sdk).
If you are developing games - then googles snake example is a good one to start.

If you have a sample android project (new android project) in eclipse  then - Copy googles sample
code for Snake into your project - and then remove main.xml from res/layout.
(or preferably use New Android Project ->Create project from existing sources)

You should now be able to run it on your device(like incredible) or emulator..
For me the emulator took to much time and I gave up. On incredible it ran OK on second
attempt(after doing force crash on first).
While running it - it displayed a confusing message that I need to press on some "up button".
to start playing. I quickly realized that they meant - that i need to move the track ball up.(I hardly use the
weird track ball in droid incredible)

NOTE: If your PC (or maybe phone) is sleeping then this device may go offline.
Hence you may get below error:

[2010-06-03 13:06:23 - Demo ] java.io.IOException: device (HT0DEVICE) request rejected: device offline
To fix this just kill adb.exe (From task manager)  or try restarting eclipse.

MY FIRST SAMPLE CODE on Android(droid incredible):
I looked at googles sample code for Text To speech. It was too much details for a sample . Hence I used it to write my own sample. The full code of my Text to speech sample code is here:

http://android-java.blogspot.com/2010/05/tutorial-to-develop-text-to-speech-app.html



Deploying on HTC droid incredible :
The current google sdk(2.1 version 7) does not have incredible settings in its
USB configuration file. Hence when you connect your computer to charge it.
It may say that "ADB" could not be installed. Without it - you will not
be able to see - droid incredible - in target devices in eclipse(or adb devices will
not show anything).
I followed instructions on : http://www.flexjunk.com/2010/05/01/installing-htc-incredible-android-sd-drivers/
It did work on one of my xp machine(but strangely I could not make
it work on another XP machine.. maybe due to jdk or driver corruption).. Be careful
- in trying USB debugger to fix this(as my antivirus says that most of them have virus)


Running on droid incredible device :
After the ADB was installed- I just created default android project .
I then ran it - and it automatically chose my htc as target(as i did not set up any other emulator devices) . It also automatically installed in my device(with android market icon). I was pleasantly surprised that it ran immediately!(ie did not take forever to load/run  - unlike when you run with emulator. It may make sense to directly
test on device.
Debugging Issue: However - i made some mistake when I tried to run TextToSpeech sample provided by google sdk(after I edited it).. It did not give any good error message . All it said on the device was that - I need to press button to forcibly close the application.  Luckily - I knew the changes I made and i removed it.(it was perfectly compile application - but i had added some color stuff). Hence I would recommend that you do development in  small steps - else you wont know - why your stuff is failing.


Other notes:
  • There is no api to send automated email(you can just open email window).
However strangely there is api to send automated SMS(which can cost money
and abused by virus) For sending email you may have to use (huge size) - ported version of javamail(as javamail is dependent on awt).

  • There is facility in the api to "place phone call" . But I could not find anyway to hook - my text to speech sample into it.(ie can not leave automated telephony message)

        This will also allow you to delegate UI updates to be controlled on server.(as most users will not update apps). This is a good approach for many apps. The security etc are being handled by traditional mechanism.
  • Once you install sdk - you can use ddms.bat from tools directory - > Choose a device -> and do device capture to do screen shots of stuff in your phone.
  • If you want to develop cross mobile OS apps - and using HTML/JS then you may consider Titanium Appcelerator or PhoneGap
         http://jonathanstark.com/blog/2010/04/05/phonegap-vs-titanium-mobile-et-al/
(initial impression is that Titanium is more focussed on IPhone/Android . But PhoneGap which provides little framework is truly more portable)


Other related Blog:
You may click on my blog - http://mobile-web-html.blogspot.com/ for steps/tips on creating mobile website for android,iphone. (as not many users will like
to download apps - as they can use alarms/services to impact battery life)

You can click this : choose android phone/usage/tips link to learn(or go back) more about deciding on how to choose best android phone. (developers should prefer  phone that is easily rootable - to test
your app on different/upcoming android versions ). You may click here for droid incredible specific review/tips/usage.

Wednesday, September 1, 2010

Tutorial to develop text to speech app on Android




This tutorial will show how to develop simple Text to speech application on
android(tested on htc droid incredible).  It assumes that you have already
managed to develop hello world(or default project ) in Eclipse per http://android-java.blogspot.com/

  • Step 1 - Create default android project in Eclipse. Run it to make
sure it works.

  • Step 2- Replace below code in your main(activity class). You may need to change package and class name.(This code is tweak of sdk sample - ie its simplified )

  

package com.test;//change this
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.test.R; //change this
import java.util.Locale;
import java.util.Random;
public class TestAct extends Activity implements TextToSpeech.OnInitListener {
private static final String TAG = "TextToSpeechDemo";
private TextToSpeech mTts;
private Button mAgainButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize text-to-speech. This is an asynchronous operation.
// The OnInitListener (second argument) is called after initialization completes.
mTts = new TextToSpeech(this,
this //TextToSpeech.OnInitListener
);
mAgainButton = (Button) findViewById(R.id.again_button);
mAgainButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sayHello();
}
});
}
@Override
public void onDestroy() {
// Don't forget to shutdown!
if (mTts != null) {
mTts.stop();
mTts.shutdown();
}
super.onDestroy();
}
// Implements TextToSpeech.OnInitListener.
public void onInit(int status) {
// status can be either TextToSpeech.SUCCESS or TextToSpeech.ERROR.
if (status == TextToSpeech.SUCCESS) {
int result = mTts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA ||
result == TextToSpeech.LANG_NOT_SUPPORTED) {
// Lanuage data is missing or the language is not supported.
Log.e(TAG, "Language is not available.");
} else {
// Check the documentation for other possible result codes.
// For example, the language may be available for the locale,
// but not for the specified country and variant.
// The TTS engine has been successfully initialized.
// Allow the user to press the button for the app to speak again.
mAgainButton.setEnabled(true);
// Greet the user.
sayHello();
}
} else {
// Initialization failed.
Log.e(TAG, "Could not initialize TextToSpeech.");
}
}
private static final Random RANDOM = new Random();
private static final String[] HELLOS = {
"Hello World",
"This is Text to speech demo by Zahid Shaikh"
};
int i =0;
private void sayHello() {
// Select a random hello.
int helloLength = HELLOS.length;
String hello = HELLOS[i];
i++;
if(i == helloLength) i =0;
mTts.speak(hello,
TextToSpeech.QUEUE_FLUSH, // Drop allpending entries in the playback queue.
null);
}
}
  • Step 3 -   Change res/main/layout.xml (or equivalent)
<linearlayout android:background="#ff00ff" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">
    <button android:enabled="false" android:id="@+id/again_button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:minlines="3" android:text="@string/again" type="submit"> </button>


</linearlayout>

  • Step 4 - change res/values/string.xml
<resources>
    <string name="hello">hi world</string>
    <string name="app_name">test</string>
<string name="again">Again</string>
<string name="red">red</string>

<string name="white">#FF00FF</string>
</resources>

  • Run your code - and press on the again button!!! 

Monday, July 12, 2010

Google App Inventor tutorial/overview for beginner

OVERVIEW :
Googles app inventor is a drag and drop web(html + java webstart) based IDE(not eclipse based)- which aims to simplify  building of apps.
For techies this is(almost) like equivalent of Visual basic, while Eclipse based android  development is like equivalent of visual c++/c#.

BASIC STEPS:
The basic step is to design(buttons) screen using web(html) based IDE. Next step is to add events(click) for the components(buttons) using Blocks editor which uses  JNLP(java webstart) launcher. You may then add events to your GUI or click the connect to phone button - on block editor to test your app .

 App Inventor (unfortunately) does not generate/emit java source code - and hence it may only be good for educational purpose(or small customizations).
You can get started using : http://appinventor.googlelabs.com/about/ (though beta version is not offered to everyone). If you dont have an actual phone then you
may try build app using simulator(very very slow) using this link: http://appinventor.googlelabs.com/learn/reference/other/emulator.html.

Getting Started with Hello world example :

a)Setup your phone and computer:
You will need to have java and "app launcher extras"(google sdk+ ) installed on your machine. (for droid incredible etc you have to add USB related settings in the sdk driver inf file as mentioned in below step )

http://appinventor.googlelabs.com/learn/gettingstarted.html


b) Go to appinventor.googlelabs.com . Click on MyProjects link on RHS. I am assuming that your google user Id has
permissions to use app inventor , you will also need to accept the T&C(first time). You will see window like below.
Drag a textbox, button and label as shown below. You can click on Properties on RHS to change button/label text.
If you need to delete something then you may use the delete button below components column. You may note that
if you change labels then it may not immediately get refreshed. After making changes click on "Save" button on top.
Now click on Open blocks editor button on top and download/accept any JNLP permissions to start it.
Since this IDE is HTML(not thick client) - it will not be user friendly(or fast) as compared with the "Blocks editor"
(which uses Java web start .. However java web start is harder to install )

(Click on above picture to see full image)



b) Let us add code/events so  that if button is clicked - then we will set labels text - to whatever values was typed in textbox.

Click on Button1 and drag "when button1.click" event. Similarly as shown below click on "Label1" and "Textbox1"
events.You may click on connect to phone to test the app. After testing - you can close block editors - and click on "package
for phone" button on top rhs to permanently deploy this sample on your phone.


(Click on above picture to see full image)


ISSUES: You may have trouble(eg could not create java runtime) running the launcher - and hence you may download (save) the JNLP file , edit it (eg remove j2se tag)- and then run it(or you may need to configure java control panel for proxy server settings).
For droid Incredible you should connect the USB/phone in "charge only"(not disk mode), else you will have error like "error uploading/connecting to media"(in short avoid app or settings which will automatically mount the drive) . For other phones
you may need the mass storage options.
You can then go back to Web based ide - and click on package for your phone - to install the app.
For other issues refer to : http://appinventor.googlelabs.com/learn/troubleshooting.html

Next Steps: Try some tutorials from google :
http://appinventor.googlelabs.com/learn/tutorials/index.html

Reference manual : http://appinventor.googlelabs.com/learn/reference/

Invoking external API (or Webservice) :
Try the experimental TinyWebDB to invoke your webservice(TinyWebDB
is almost like a  hashtable over web - and supports only storeAValue and getValue
operation - and hence you may have to do some hacks or mapping -
to call external service):
http://appinventor.googlelabs.com/learn/reference/other/tinywebdb.html
http://www.appinventor.org/talking-to-an-api

In future google app inventor components can be developed using java per
http://appinventor.googlelabs.com/learn/userfaq.html
(In that case it will be similar to developing components in VC++ to make VB
lot more productive/easier to use)
Similarly - mulitple screen can be deveopled via a hack.
Online Book  :
There is a text book link(outdated) on LHS at
http://www.appinventor.org/

Developing app using java using Android SDK:
You can click here to go back - to the beginning of this blog to develop android
app using java(and eclipse).(instead of using app inventor)

Others:
If  you are confused about various android phone - then my opinion is at:
http://bestandroidphone.blogspot.com/ (though cnet.com will be better from specs perspective
But they wont tell you bugs in phone.)
My index of technical blog is at :
http://www.jroller.com/zahid eg selenium tutorial, seam etc