Archive for the ‘eclipse’ Category

Tips of Eclipse for Android

May 30, 2010

1. Override/Implement methods (actually it declares those methods for you to implement:))

In the “Package Explorer” -> Right click the class ->

Or simply “Alt+Shift+S” to open the “Source” menu,  Or choose “Source” from the menu->

then choose “Override/Implement Methods”

Multiple offline devices attached

September 10, 2009

This was driving me crazy this morning as I was debugging using G1 phone on Eclipse. Basically the Android AVD Manager sees multiple offline devices attached, although they are all the same device (sometimes it sees one online device sometimes none online device); despite that “adb devices” reports the correct info about device attached.

So basically Mr. Mark Murphy’s suggestion solved my problem:

1. Unplug all devices from computer and shut down all instances of emulators;

2. Run “adb kill-server“;

3. Run “adb devices” and you should see an empty list of devices;

4. Plug back in your device or re-lauch emulator. Now hopefully AVD Manager should now act normally.

Building mix-up custom Android component/widget using Java class and XML layout

August 27, 2009

A lot of times you would find it convenient to just create “compound components” or what Commons guy called “meta” widgets.These widgets are compositions of other widgets. Most of the work involved would be to layout those compound components, bind data and wire up controls. For the layout part, it’s convenient to just use mixture of a Java class and a corresponding xml layout file, as opposed to using a pure Java class. Here is how:

1. Create the Java class. It usually extends a Layout class such as a LinearLayout or a RelativeLayout.  You have to define a constructor in the flavor of

public MyHomeBrewedComp(Context context, AttributeSet attr)

If you just use "public MyHomeBrewedComp(Context context)“,  you would get an “java.lang.NoSuchMethodException" error from Console and a “android.view.InflateException: Binary XML file line #nnn: Error inflating class com.mh.android.test.MyHomeBrewedComp" from CatLog at runtime.

2. Create the corresponding xml layout file under the package “res/layout” folder.  This is the xml version of your Java class, which means the root node should be the identical class to your Java class.

3. Layout compound widgets in your enclosing widget. If you are doing layout in the xml file, you will then have to inflate the xml in your java file programmatically. You do this by overriding the widget’s onFinishInflate () method.

@Override
protected void onFinishInflate(){
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.myhomebrewedComp, this);
//other code comes here, such as wiring up controls
...
}

4. TODO: setting up attributes in the custom widget

5.  Using the widget in XML.  First of all you will need a new namespace when you refer to any property/attribute in your widget; second you have to use the full-qualified class name in the xml file. Here is a sample:

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:app=”http://schemas.android.com/apk/res/com.mh.android.test&#8221;
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>

<com.mh.android.test.MyHomeBrewedComp
android:id=”@+id/panel”
app:velocity=”30″
app: friction=”.7f”
android:orientation=”horizontal”
android:layout_width=”fill_parent”
android:layout_height=”75px”
android:layout_alignParentTop=”true”
/>

</RelativeLayout>

Synchronizing small projects in eclipse

August 17, 2009
  • To export the project as a zip: File>General>Archive File;
  • To import it back to the workspace: File>General>Existing Projects into Workspace
    and select the aforementioned zip file. This will result in a new folder in the root of the current workspace.

JumpStart Android 101 for absolute beginners

June 19, 2009

Running the examples that come with the SDK in Eclipse

Eclipse->Package Explorer->Right click->New ->Android Project->Contents->Create project from existing source->Location>Navigate to

<directory_of_your_android_SDK>/platforms/android-1.5/samples/ApiDemos>OK

Eclipse will populate other setting values such as “Project name” and “Build Target” for you.:)

Local resource page (index.html)

<directory_of_your_android_SDK>/docs/index.html

just in case you don’t have/just lost/are having creeping internet but are desperate to look up the documentation to get something done ASAP so that you can go home;)

Using Android SDK 1.5 emulator with proxy in Eclipse 3.45

June 19, 2009

Go to Package Explorer -> Right click your Android project ->Run As->Run Configurations.

Under Android Application on the left column, select your project -> on the right column, where you see Android | Target | Common tabs ->

Select Target -> on the bottom “Additional Emulator Command Line Options”->

-http-proxy http://wwwgateProxy.com:1080 -debug-proxy http://wwwgateProxy.com:1080

->Run/Close.

JSON to XML

June 1, 2009

A lightweight plugin for Eclipse. Works good for me on Flex Builder 3 (v3.0.1.2x).

JavaScript xml2json and json2xml.

Android SDK 1.5 with Eclipse 3.4 on Ubuntu 8.x

May 23, 2009

1. I am on Ubuntu 8.10 which seems to only support Eclipse 3.2 from Synaptic Package Manager. Since I installed both Eclipse and the IcedTea OpenJDK/JRE from Synaptic, I found out later that Android Dev tool actually needed the Sun JDK in order to use its Layout Editor (otherwise you won’t be able to use Android Layout Editor to edit xml files under res/layout folder. The error was java.awt.font wasn’t found something like that.) What I ended up doing was I uninstalled both Eclipse and IcedTea distribution of JDK (openjdk-6-jdk) from Synaptic, otherwise the newly updated Eclipse will continue to use openjdk which prevented me from using Android Layout Editor.

2. Install Sun’s Java (both jre and jdk).

Every step described in the above article worked for me except I couldn’t get the first step work which was:

sudo apt-get install sun-java6-jdk

apt-get said it could not locate sun-java6-jdk something like that.

So I went with this:

sudo aptitude search sun-java

Which told also me it wasn’t able to find sun-java first time I ran it. But after I did

sudo aptitude

and ->Actions->Update package list

I got to install sun java:

sudo aptitude install sun-java6-jre sun-java6-jdk sun-java6-plugin sun-java6-fonts

I was very happy with aptitude as compared to Synaptic package manager because of the exotic flavor of the former. Besides it seems to be more stable and responsive to me based on my judgement.;)

However, during the adventure, I got confused as to how I could single/full-heartedly accept the license agreement which showed up as a pop up within shell. It turned out  I had to maximize the whole thing and scroll down and use TAB key to select “OK” in order to advance.

After all this settled down, I rebooted Ubuntu and made sure that icedTea no longer took over the java world:

java -version

I get

java version "1.6.0_13"

Java(TM) SE Runtime Environment (build 1.6.0_13-b03)

Java HotSpot(TM) Server VM (build 11.3-b02, mixed mode)

3. Install and setup Eclipse 3.4.

I manually downloaded Eclipse 3.4 from their site and unzip it into /usr/lib folder. Android dev page recommends Eclipse IDE for Java EE Developers, Eclipse IDE for Java Developers, or Eclipse for RCP/Plug-in Developers.

You may want to temporarily change the owner of the/usr/lib folder to yourself instead of root:

chown <user> /usr/lib

After you are done, go to Eclipse->Help->About Eclipse Platform->Configuration Details, you can see Eclipse is now using the Sun jdk/jre:

java.library.path=/usr/lib/jvm/java-6-sun-1.6.0.10/jre/lib/i386/client:/usr/lib/jvm/java-6-sun-1.6.0.10/jre/lib/i386:/usr/lib/xulrunner-addons:/usr/lib/xulrunner-addons:/usr/lib/xulrunner-addons:/usr/java/packages/lib/i386:/lib:/usr/lib

java.runtime.name=Java(TM) SE Runtime Environment

java.runtime.version=1.6.0_10-b33

and

java.awt.graphicsenv=sun.awt.X11GraphicsEnvironment

4. Install Android Development Tool for Eclipse. I personally found/suspected that the http url is faster/more stable(?) than the https one, esp if you are in slow connection. If you are behind some firewall, you may want to set Eclipse’s proxy. Eclispe->Window->Preferences->General->Network Connections->…

5. Configure BASH to export the JAVA_HOME env variable, as well as path to Eclipse like so:

sudo gedit /home/<user>/.bashrc

# ~~~~~~~~~~~~~~~~~ JAVA (JDK,JRE) ~~~~~~~~~~~~~~~~~~~~~

export JAVA_HOME=’/usr/lib/jvm/java-6-sun-1.6.0.10′

export ANDROID_HOME=’/usr/lib/android-sdk-linux_x86-1.5_r2′

PATH=.:$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$ANDROID_HOME/tools

alias eclipse=’java -jar /usr/lib/eclipse/startup.jar’

6. (Optional) Configure Eclipse JRE to the one we just installed from Sun. Eclipse->Window->Preferences->Java->Installed JREs->Add->Standard VM->/usr/lib/jvm/java-6-sun-1.6.0.10. And check this new one we just added.

7. You may want to run

Eclipse -clean

if you had some trouble with it during the process.

8. I guess that’s pretty much about it(?)!

9. Have a great weekend/holiday!

Android on Eclipse 101: Google MapView

May 21, 2009

In order to get MapView, you will need to first have your application target Google API 1.5. Then you would abtain a google maps API for your android application:

1.  Generating MD5 Fingerprint of the SDK Debug Certificate . This is under Windows XP:

cd C:\Program Files\Java\jdk1.6.0_13\bin
keytool -list -alias androiddebugkey -keystore “C:\Documents and Settings\username\.android\debug.keystore” -storepass android -keypass android
I got something like:
androiddebugkey, May 18, 2009, PrivateKeyEntry,
Certificate fingerprint (MD5): 94:1E:43:49:87:73:BB:E6:A6:88:D7:20:F1:8E:B5:98
Then I go to here:
http://code.google.com/intl/ja/android/maps-api-signup.html
to get the api key.
2. Now you can get the map view up and running. The catch is that it  seems that you need to use/subclass MapActivity in order to use a MapView. The MapView could be mixed up with other views (not necessarily need to be the top node in your view layout). Also you need to declare in your manifest.xaml that you are using google map and internet permission. For example:

<!–Manifest xml–>
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.maohao.android.test”
android:versionCode=”1″
android:versionName=”1.0″>
<uses-sdk android:minSdkVersion=”4″
android:targetSdkVersion=”4″ />
<application android:icon=”@drawable/icon” android:label=”@string/app_name”>
<activity android:label=”@string/app_name” android:name=”MyTestMain”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.googlemaptest.MapTest1″></activity>
<uses-library android:name=”com.google.android.maps” />
</application><uses-permission android:name=”android.permission.INTERNET” />
</manifest>

<!–/res/layout/main.xml–>
<?xml version=”1.0″ encoding=”utf-8″?>
<FrameLayout
xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@+id/container”
android:layout_width=”match_parent”
android:layout_height=”match_parent”>

<WebView
android:id=”@+id/webview”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
/>

<com.google.android.maps.MapView
android:id=”@+id/mapview”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:clickable=”true”
android:apiKey=”0P-lFg3oLM1s4ffK1LiMkkFi9dJ4mFvCcd8oELA”
/>
</FrameLayout>

package com.maohao.android.test.googlemaptest;
//import relavent classes
//,..
public class MapTest1 extends MapActivity {

WebView mWebView;
MapView mMapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flipview_main1);

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(“http://www.google.com&#8221;);

mMapView = (MapView) findViewById(R.id.mapview);

}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

“Unexpected end of token stream when parsing XML”

April 29, 2009

When I tried to load an external xml file using an XML tag, I got something like this from Flex Builder:

“Problem parsing external XML. Unexpected end of token stream. The last token was: 360.”

The file was built in Nodepad++ and was displayed fine in Firefox as well as in Eclipse/FB. When I got the error message, I then tried to load it in IE, which also gave me an error. What I did to fix it was going back to Nodepad++ and  “Format>Encode in UTF-8“, which basically changed the encode from ANSI (the default one for Notepad++) to Unicode. I got several “special” unicode characters such as 360° in my orignial XML file.