Java – How to run JavaFX and make an ImageView in Android Studio

How to run JavaFX and make an ImageView in Android Studio… here is a solution to the problem.

How to run JavaFX and make an ImageView in Android Studio

I have an interface to the javafx.scene.image.Image class. I have to implement it and create the ImageView. So, how do I add JavaFx to my Android Studio project and how do I execute an ImageView or other image element in my activity?

User .java

import javafx.scene.image.Image;

public interface User
    {
        String getName();
        int getId();
        Image getIcon();
    }

Solution

Based on JavaFXPorts and Kokos sample you can add JavaFX to Android projects created with Android Studio, and you will be able to create them in Android fragments runs JavaFX scenario.

Note that JavaFXPorts was originally designed to run full JavaFX projects on Android and iOS using pure Java without the need for native code. And vice versa, but it’s not the best approach.

In any case, these are the necessary steps to create a JavaFX scene using JavaFX ImageView to save JavaFX Images.

1。 Create a simple project on Android Studio

Choose a Basic Activity template and choose to use a fragment.

Add a fragment

2。 Modify the build gradle file

build.gradle (top-level project).

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'me.tatarka:gradle-retrolambda:3.5.0'
        classpath 'com.android.tools.build:gradle:2.2.3'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle (module: application).

In this file, we will include dalvik-sdk, Android’s JavaFXPorts dependencies. If you use the Java IDE’s Gluon plugin to create a mobile project through the jfxmobile plugin, you can find it installed in:

~/.gradle/caches/modules-2/files-2.1/org.javafxports/dalvik-sdk/8.60.10/e... a/unpacked/dalvik-sdk

Otherwise, it can be downloaded from here. Extract it to a given path, let’s say $dalvikPath.

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.3"
    defaultConfig {
        applicationId "gluonhq.com.myfragment"
        minSdkVersion 25
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    dexOptions {
        preDexLibraries = false
        additionalParameters=['--core-library']
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main {
            jniLibs.srcDir file("$dalvikPath/rt/lib")
            assets.srcDirs = ['assets']
        }
    }
    lintOptions {
        abortOnError false
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile fileTree(include: ['*.jar'], dir: '$dalvikPath/rt/lib/ext')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
    compile 'com.android.support:design:26.0.0-alpha1'
    testCompile 'junit:junit:4.12'
}

3。 Create a class that extends FXFragment

In MainActivity.java:

public static class PlaceholderFragment extends FXFragment {

public PlaceholderFragment() {
        String fxapp = "your.package.myfragment.MainFX";
        this.setName(fxapp);
    }

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

And call it from onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
    ...
}

Note that android:id="@+id/container" is required in activity_main.xml.

4。 Final step: Create a JavaFX class

Make sure the package name is your.package.myfragment.

MainFX.java

public class MainFX extends Application {

@Override
    public void start(Stage stage) throws Exception {

final ImageView imageView = new ImageView();
        imageView.setImage(new Image("https://upload.wikimedia.org/wikipedia/en/c/cc/JavaFX_Logo.png"));
        imageView.setPreserveRatio(true);

Screen primaryScreen = Screen.getPrimary();
        Rectangle2D visualBounds = primaryScreen.getVisualBounds();
        double width = visualBounds.getWidth();
        double height = visualBounds.getHeight();

StackPane stackPane = new StackPane(imageView);
        Scene scene = new Scene(stackPane, width, height);

imageView.fitWidthProperty().bind(scene.widthProperty().subtract(20));
        stage.setScene(scene);
        stage.show();
    }

}

Note that this is a pure JavaFX application class and you need to set the stage size based on the screen boundaries.

Deploy and run the app

Note that you will be running JavaFX in Android View, but depending on your layout, you still have other Android components in different views on top of it.

Related Problems and Solutions