Friday, November 29, 2013

JavaFX on Android

Hi there,

Today I finally was able to get one of my JavaFX custom controls running on my Android based Nexus 7 and I think this is a good reason to share it with you...
So first of all I have to say THANK YOU to Johan Vos for setting up a Bitbucket Project that makes the setup very easy. So for those of you that don't know what I'm talking about, please take a look here

https://bitbucket.org/javafxports/android/wiki/Home

There you will find a description how to compile a JavaFX project for Android. Well for those of you that know me you can imagine that running a "boring" standard JavaFX application is fine but I would like to see one of my controls running on my Nexus 7 :)
So the first thing I did was trying to get one of my Enzo controls running on my Nexus 7 and what should I say...I failed... :(
Well that was not really surprising because you need JDK7 style Custom Controls to let them run on your Android device. 
That means you usually would need

  • Control
  • Skin
  • Behavior
  • CSS

for your control. So I backported the SimpleGauge from Enzo to JDK7 and tried to get it running on the Nexus 7...and again failed.

The only thing that I saw was a little white square on the Nexus which looked to me like the scene was not properly resized. The next step was instantiating the scene directly with a with and height and now...I saw a white box with the right size but without any control...

This reminded me on the problems that I had with JavaFX Custom Controls on the BeagleBoard xM and so I used a different approach which is extending Region to create the control.

So instead of using the Control, Skin, Behavior and CSS approach I now simply created a class that extends Region and contains all the properties and drawing code.
And what should I say...it worked as you can see on this little video that I recorded with my iPad mini.





UPDATE: 
I've updated the app and instead of using random numbers it now shows live measured temperature, humidity and pressure data from one of my Raspberry Pi's. Btw the video also shows the same code running on my iPad mini :)

Here is the link: https://www.youtube.com/watch?v=KqSjywExsTs

So there are a few things I would like to tell you that might lead to problems...

Creating the jar from the IDE didn't work for me so I created the jar by using the javafxpackager like follows


javafxpackager -createjar -appclass PACKAGES.AND.YOUR.MAIN.CLASS -outfile YOUR_JAR_NAME.jar -v -nocss2bin -srcdir /PATH/TO/YOUR/PROJECTS/CLASS/FILES

That creates a jar file that you can start on the console by calling
java -jar AndroidFX.jar

After that was done I followed the instructions from Johan Vos and here you should use Gradle 1.4 (I got problems when using Gradle 1.9).

And another important thing was that I have to use JDK7 as the standard VM when compiling everything.


So for those of you that would like to try the Android APK file directly you can download it here:

AndroidFX-debug.apk

And for those that would like to take a look at the code that I used, feel free to fork it on Bitbucket...

https://bitbucket.org/hansolo/androidfx

I think that's all I have to say for today, so enjoy your upcoming weekend and as always...keep coding...

2 comments:

  1. Very nice! Congrats!
    The issue with Gradle 1.9 might be resolved in the new build.
    Did you get an error concerning createPackage?

    ReplyDelete
    Replies
    1. Hi Yuri,
      Exactly that was the error. Looking forward to future builds :)
      Cheers,
      Gerrit

      Delete