Using Android API's in JavaFX.

In a previous blog entry, I talked about the JavaFX to Android porting project. Now that this seems to be on the rails, it's time for some more integration. In this blog post, I will quickly show how you can use the Android API's in your JavaFX application.

Before showing the code, I should stress that the current way of calling Android services from a JavaFX application is probably not the recommended way in the future. I am looking into better ways, but for now, this is the way it works.

We will create an example that retrieves and prints the location of the user (or the device, that is).


We assume you have the Android SDK and the JavaFX-Dalvik runtime, as explained in
You have to create a JavaFX project. I use Netbeans 7.4 beta, and create a new JavaFX Application. The code of my application is here:

package locationandroid;

import android.location.Location;
import android.location.LocationManager;
import android.location.LocationProvider;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class LocationAndroid extends Application {

    private LocationManager lm;
    private String provider;

    public void start(Stage primaryStage) {
        final Label l = new Label ("Location");
        FXActivity f = FXActivity.getInstance();
        if (f != null) {
            Object systemService = f.getSystemService(FXActivity.LOCATION_SERVICE);
            lm = (LocationManager)systemService;
            List providers = lm.getAllProviders();
            if ((providers != null) &&(providers.size()>0)) {
                this.provider = providers.get(0);
        Button btn = new Button();
        btn.setText("Retrieve location");
        btn.setOnAction(new EventHandler() {

            public void handle(ActionEvent event) {
                Location loc = lm.getLastKnownLocation(provider);
               l.setText("Location: "+loc.getLatitude()+";"+loc.getLongitude());
        VBox all = new VBox();
        all.getChildren().addAll(btn, l);
        StackPane root = new StackPane();
        Scene scene = new Scene(root, 300, 250);
        primaryStage.setTitle("JavaFX/Android Location");

In the NetBeans project, we added 2 libraries:

  • android.jar, which is located in the Android SDK
  • jfxdvk.jar, which is located in the dalvik-sdk/rt/libs directory

You need the android.jar in order to use the Android API's, and you need jfxdvk.jar in order to access the FXActivity class, which is the bridge between JavaFX and the Dalvik runtime. We use a static method on the FXActivity to retrieve the FXActivity, which extends the Android Context. This Context can be used to lookup Android services. As you can see, we obtain the Android LocationManager by calling


The obtained LocationManager can be used to retrieve a list of LocationProviders. We just take the first one, and will use that to query the LocationManager and retrieve the current location whenever the user presses the "Retrieve Location" button.

When you want to run this, you need to ask permission to obtain the location. The AndroidManifest.xml file that is created when setting up your javafx android application needs to have this line:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>



Again, this is just a very quick sample and it is not showing any best practices, but hopefully you can now start writing JavaFX Applications that leverage the Android API's.

written on 22 Feb 2014 17:50.


Author: Ashish Agre
Date: 24 Feb 2014 04:48

I was waiting for this. Will other Android APIs also work in similar manner? And do we have to add android.jar from android sdk to our javafx web application?

Author: Ashish Agre
Date: 24 Feb 2014 04:49

Oops Sorry Javafx App*

Author: Tomas
Date: 24 Feb 2014 10:29

I actually wonder how Johan did it. When you're developing an javafx application with android api you need to have android.jar on classpath but then you are compiling android application android.jar is on the bootclasspath by default.

Author: wade
Date: 07 Mar 2014 10:24

Very very glad that JavaFx can be ported to Android.

Author: Paul Newman
Date: 28 Apr 2014 08:39

Hi .. We are developing our first android app and thought that the javafx route was an excellent one, but we need to take a photo using the camera from the app. We couldn't get it working and so have started to look at the native android sdk. We'd love to use javafx, so can anyone help us to get the camera working reliably ? Thanks Paul Newman

Author: Reed Richards
Date: 28 Dec 2015 17:52

Paul, did you ever gain access to the Android camera through JavaFX? Has anyone else?

Author: Alex Day
Date: 05 Feb 2016 09:04

I am also interested if anyone else has had luck with this.

Author: Thomasf2220
Date: 08 Sep 2016 08:40

This is great information. JavaFX on Android is the best thing ever. Hope the working is as good too. :)

Author: Len
Date: 22 Mar 2017 08:29

My device (POS) has dual monitors. - Use Native SDK of Android: Android can detect 2 monitors by using DisplayManager-> displayManager.getDisplays() To switch between 2 monitors we use Presentaion(context, display). - Use javafx on Window -> easy to use Screen.getScreens() port to Android (javafxports), Screen.getScreens() doesn't work, can not detect 2 monitors (2 screens) -> only one screen. How to solve this problem on Android using javafxport. Many thanks!

Create comment