So I have seen this article about it and it solves a bit of the problem I guess. Link: similar problem here
However, I am using AdMob and Firebase integration and therefore I use an AdView, AdRequest and MobileAds etc. I use these in the AndroidLauncher and that works fine, but when trying to implement an interface called AdsManager and creating a variable of type AdView it can't resolve it. I guess it is because I am out of the android module. So how can I get an "AdMob type" variable from AndroidLauncher when I'm in the Core module (MyGdxGame)?
Example: Android module
AdsManager
package com.game.mygame.monetization;
public interface AdsManager {
void showAds(boolean show);
}
AndroidLauncher
public class AndroidLauncher extends AndroidApplication implements AdsManager {
private final String TAG = "AndroidLauncher";
private AdHandler adHandler; // Variable I want to reach from core module
static class AdHandler extends Handler {
private final int ADS_SHOW = 1;
private final int ADS_HIDE = 0;
private AdView adBanner; // Variable I want to reach from core module
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case ADS_SHOW:
adBanner.setVisibility(View.VISIBLE);
break;
case ADS_HIDE:
adBanner.setVisibility(View.GONE);
break;
}
}
}
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
RelativeLayout layout = new RelativeLayout(this);
View view = initializeForView(new MyGdxGame(this), config);
layout.addView(view);
implementAds();
requestAds(layout);
// Sets the background image
setContentView(layout);
}
public void implementAds() {
// Implement ads
MobileAds.initialize(this, "");
adHandler = new AdHandler();
adHandler.adBanner = new AdView(this);
adHandler.adBanner.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Log.i(TAG, "Ad loaded ...");
}
});
adHandler.adBanner.setAdSize(AdSize.SMART_BANNER);
adHandler.adBanner.setAdUnitId("");
}
public void requestAds(RelativeLayout layout) {
AdRequest.Builder builder = new AdRequest.Builder();
builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
builder.addTestDevice("");
RelativeLayout.LayoutParams adBannerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layout.addView(adHandler.adBanner, adBannerParams);
adHandler.adBanner.loadAd(builder.build());
}
@Override
public void showAds(boolean show) {
adHandler.sendEmptyMessage(show ? adHandler.ADS_SHOW : adHandler.ADS_HIDE);
}
}
When trying to create a class called AndroidAdsManager that implements AdsManager it can't resolve a private AdView variable because I'm no longer in the android module I guess.
Core module
public class AndroidAdsManager implements AdsManager {
private AdView adBanner; // Error: Android Studio doesn't find this type
private AdHandler adHandler; // Also needs this in here, I guess, maybe not
@Override
public void showAds(boolean show) {
adHandler.sendEmptyMessage(show ? adHandler.ADS_SHOW : adHandler.ADS_HIDE);
}
}
Long story short: You can't
Hello Kevvex, There´s no way you can access AdView or AdHandler from classes in the core module, what you need to do to work with them is (As shown in the link you provided) have all your logic implemented in the AndroidAdsManager class that implements your AdsManager interface
The way I would go about this is, whenever you want something to be done with types from the Android module, have a methdod in the AndroidAdsManager do that something, you will need to create the methods in the AdsManagerInterface in order to call them:
AndroidAdsManager
public class AndroidAdsManager implements AdsManager {
private AdView adBanner;
private AdHandler adHandler;
@Override
public void showAds(boolean show) {
adHandler.sendEmptyMessage(show ? adHandler.ADS_SHOW : adHandler.ADS_HIDE);
}
@Override
public void implementAds() {
// Implement ads
MobileAds.initialize(this, ""); // HERE YOU NEED TO GET THE CONTEXT
adHandler = new AdHandler();
adHandler.adBanner = new AdView(this);
adHandler.adBanner.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Log.i(TAG, "Ad loaded ...");
}
});
adHandler.adBanner.setAdSize(AdSize.SMART_BANNER);
adHandler.adBanner.setAdUnitId("");
}
// You need to get a RelativeLayout reference for this method to work
// I would get it from AndroidLauncher
public void requestAds() {
AdRequest.Builder builder = new AdRequest.Builder();
builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
builder.addTestDevice("");
RelativeLayout.LayoutParams adBannerParams = new
RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layout.addView(adHandler.adBanner, adBannerParams);
adHandler.adBanner.loadAd(builder.build());
}
}
AdsManager
public interface AdsManager {
void implementAds();
void requestAds(); // Relative layout param removed from here
void showAds(boolean show);
}
After this, if you want these operations to affect your game, simply pass the reference of your game instance (core module) to the AndroidAdsManager
from the AndroidLauncher
to be able to call any method you want from there, hope this helps!