Search code examples
javaandroidjavafxportsgluon-mobile

Can I display google adwords (AdView) in javafx on android


I have a javafx app running on android and would like to place google adwords on some of the screens. The issue I have is that the adwords use googles AdView to display the add which is an Android presentation object and is not compatible with the JavaFX presentation layer.

Sample code that creates AdView

import com.google.android.gms.ads.MobileAds;

import javafxports.android.FXActivity;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

/**
 * Constructor creates AdView
 * Call getAdView() to retrieve AdView object.
 */
public class GoogleAds {

    private AdView adView = null;
    public GoogleAds() {
        AdView mAdView = createAddView();
        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)        // All emulators
                .build();
        mAdView.loadAd(adRequest);
    }
...

Now I have a com.google.android.gms.ads.AdView object ready for presentation

I can add it to the android view hierarchy using

private void includeGoogleAdView(LinearLayout layout) {
    AdView adView = GoogleAds().getAdView();
    layout.addView(mAdView);
}

All this works as expected when run as an android app.

So the question is "can I get an AdView to show in the javafx presentation layer?".

Is there was a way to make an com.google.android.gms.ads.AdView become an instance of javafx.scene.Node then I could add it using something like

private Node addToPane(AdView adView) {
    javafx.scene.layout.Pane pane = new javafx.scene.layout.Pane();
    pane.getChildren().addAll( canvas, adView);
    return pane;
}

Or is there another solution?


Solution

  • You can easily include an AdView in your app by getting access to the ViewGroup from the FXActivity.

    This is a code snippet that works for me. Notice it has to run in the UIThread:

    public AndroidAds()  {
    
        FXActivity.getInstance().runOnUiThread(() -> {
            LinearLayout layout = new LinearLayout(FXActivity.getInstance());
            layout.setVerticalGravity(Gravity.BOTTOM);
            layout.setOrientation(LinearLayout.VERTICAL);
    
            AdView adView = new AdView(FXActivity.getInstance());
            adView.setAdSize(AdSize.SMART_BANNER);
            adView.setAdUnitId("ca-app-pub-XXXXXXXXXXXXXXXXXXXX");
    
            AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .addTestDevice("XXXXXXXXXXXXXXXXXXXX")
                    .build();
            adView.loadAd(adRequest);
            adView.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    super.onAdLoaded();
                    System.out.println("AD loaded");
                }
            });
    
            layout.addView(adView);
    
            FXActivity.getViewGroup().addView(layout);
        });
    
    }