I have read few articles on trying to stop adView when the app is hidden/minimised but this crashes my app.
This is my code, adView.LoadAd... and adView.stopLoading both crashes the app on startup.
public class MainActivity extends Activity implements OnItemSelectedListener {
#Override
protected void onResume() {
super.onResume();
if (AdViewStarted = true) {
adView.loadAd(new AdRequest());
}
}
#Override
protected void onPause() {
super.onPause();
if (AdViewStarted = true) {
adView.destroy();
}
}
[...]
public class AdMob extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = new AdView(this, AdSize.BANNER,"12345678901234567890");
LinearLayout layout = (LinearLayout) findViewById(R.id.adView);
layout.addView(adView);
adView.loadAd(new AdRequest());
AdViewStarted = true;
}
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
}
Thanks in advance
Replace the if statements, you have to use two equals instead of one. Correct would be
if (AdViewStarted == true) {
adView.destroy();
}
or better
if (AdViewStarted) {
adView.destroy();
}
By the win, variable names are starting with a lowercase char.
Also, what are you trying in your onCreate?
This is correct (I think, if not, show me the layout xml file and LogCat):
LinearLayout adView = (LinearLayout) findViewById(R.id.adView);
adView.loadAd(new AdRequest());
AdViewStarted = true;
Related
I've been having this problem for weeks now. I finally narrowed it down to this : the AdView's visibility seems stuck to View.GONE, no matter how hard I try to set it to View.VISIBLE.
Here is my code :
public class AndroidLauncher extends AndroidApplication {
AdView bannerView;
RelativeLayout layout;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppLovinSdk.initializeSdk(this);
AppLovinPrivacySettings.setHasUserConsent(false, this);
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); // test APP ID
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
game = new GameClass(this);
// // Create Layout that will call both Libgdx View and AdMod View
layout = new RelativeLayout(this);
// InitializeCommonRessources Libgdx View
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
//
View gameView = initializeForView(game, config);
layout.addView(gameView);
// Tell Android to use this Layout
setContentView(layout);
setupBanner();
}
#Override
public void setupBanner() {
SetupAdMobBanner();
}
public void SetupAdMobBanner() {
System.out.println("Setup AdMob BANNER");
bannerView = new AdView(this);
bannerView.setVisibility(View.VISIBLE); // These are my desperate attempts to make the banner visible
System.out.println("visibility == " + bannerView.getVisibility()); // Always prints "visibility == 8" which is the value for View.GONE (View.VISIBLE is 0)
bannerView.setAdListener(new AdListener() {
#Override public void onAdOpened() {
System.out.println("BANNER AD OPENED");
Ressources.incrementBannerClicks();
}
#Override public void onAdClosed() {
System.out.println("BANNER AD CLOSED");
}
#Override public void onAdLeftApplication() {
System.out.println("BANNER LEFT APP");
}
#Override public void onAdFailedToLoad(int var1) {
System.out.println("BANNER FAILED TO LOAD : " + var1);
destroyAdMobBanner();
SetupAdMobBanner();
}
#Override public void onAdLoaded() {
System.out.println("BANNER LOADED");
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
}
#Override public void onAdImpression() {
System.out.println("BANNER IMPRESSION");
}
});
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
bannerView.setAdSize(AdSize.SMART_BANNER);
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
bannerView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // Test banners
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_TOP);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
layout.addView(bannerView, adParams);
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
loadAdMobBanner();
bannerView.setVisibility(View.VISIBLE);
System.out.println("visibility == " + bannerView.getVisibility());
}
public void loadAdMobBanner() {
System.out.println("BANNER loadAd(adRequest)");
AdRequest adRequest = new AdRequest.Builder().addTestDevice("36429449DBD95B918CFD96E610995AE6").build(); // Test ads for my Honor 7x
bannerView.loadAd(adRequest);
}
public void destroyAdMobBanner() {
if (bannerView == null) return;
System.out.println("Destroying BANNER");
layout.removeView(bannerView);
bannerView.destroy();
bannerView = null;
}
}
As you can see, I am desperately trying to set the visibility to VISIBLE, but it always prints visibility == 8 right after (8 being the value for GONE, VISIBLE's being 0).
The weirdest thing is that sometimes, the banner will actually show up.
It might work for a day or two, and suddenly not work for a day or two, the banner won't show even though the AdListener says it's been successfully loaded, without me doing a damn thing.
The problem occurs on a physical device, a Honor 7X, with both live ads and test ads.
You have to add test device to adView. You will find test device id in logcat. If you don't add test device while development and click on ads for many times ads will stop working for that ad id.
Add only this code not set visibility
try this code
private AdView mAdView;
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// Check the LogCat to get your test device ID
.addTestDevice("36429449DBD95B918CFD96E610995AE6")
.build();
mAdView.loadAd(adRequest);
#Override
protected void onResume() {
super.onResume();
if (mAdView != null) {
mAdView.resume();
}
}
#Override
public void onDestroy() {
if (mAdView != null) {
mAdView.destroy();
}
super.onDestroy();
}
#Override
protected void onPause() {
if (mAdView != null) {
mAdView.pause();
}
super.onPause();
}
I hope that can help you!
Thank You.
I display an AdMob ad banner within a window created by a fragment. This works, but I'm getting strange leaking problems I don't understand yet. When I open and close the app a lot of times, the ad fragment is properly destroyed every time, but the MainActivity which controls this fragment is leaking:
The guilty one is this line in code:
adRequest = new AdRequest.Builder().addTestDevice(DEVICE_ID_MOTO_G).addTestDevice(DEVICE_ID_ZTE).build();
mAdView.loadAd(adRequest);
(You can see below that I already tried to nullify the adRequest, but no result.) Anyway, when I comment these two lines, the leaks don't occur. I tested it twice, because it was hard to believe that this invocation leaks, but it does. Here is the fragment code. As you can see, I even nullify everything that is possible in onDestroy(). I can assure that onDestroy() is called. loadAd starty any background thread. I guess that has to do with that problem.
public class SnippetFragment extends Fragment
{
private AdView mAdView;
private OnAdFinishedLoadingListener onAdFinishedLoadingListener;
private Context context;
private AdRequest adRequest;
private final String DEVICE_ID_ZTE = "1CA20334345BB1479C43692AFA576456487A48";
private final String DEVICE_ID_MOTO_G = "131465469A7BE11543543065404B168908CB13C8D1";
public SnippetFragment(Context context)
{
this.context = context;
}
#Override
public void onActivityCreated(Bundle bundle)
{
super.onActivityCreated(bundle);
mAdView = (AdView)getView().findViewById(R.id.adView);
mAdView.setAdListener(new AdListener()
{
#Override
public void onAdLoaded()
{
super.onAdLoaded();
if (context != null)
{
onAdFinishedLoadingListener = (OnAdFinishedLoadingListener)context;
onAdFinishedLoadingListener.onAdLoaded();
}
}
});
//THIS SOMEHOW LEAKS!
adRequest = new AdRequest.Builder().addTestDevice(DEVICE_ID_MOTO_G).addTestDevice(DEVICE_ID_ZTE).build();
mAdView.loadAd(adRequest);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_snippet, container, false);
}
/** Called when leaving the activity */
#Override
public void onPause()
{
if (mAdView != null)
{
mAdView.pause();
}
super.onPause();
}
/** Called when returning to the activity */
#Override
public void onResume()
{
super.onResume();
if (mAdView != null)
{
mAdView.resume();
}
}
/** Called before the activity is destroyed */
#Override
public void onDestroy()
{
adRequest = null;
if (mAdView != null)
{
mAdView.setAdListener(null);
mAdView.destroy();
mAdView = null;
}
context = null;
super.onDestroy();
}
}
I fortunately found the solution. You have to manually kick the AdView out of the surrounding layout, before the fragment gets destroyed. No leak anymore after I launched and closed the app about 20 times. Here is the updated fragment:
public class SnippetFragment extends Fragment
{
private AdView adView;
private OnAdFinishedLoadingListener onAdFinishedLoadingListener;
private Context context;
private LatLng routeDestination;
private AdRequest adRequest;
private LinearLayout snippetContent;
private final String DEVICE_ID_ZTE = "2353496z35752t3524267854";
private final String DEVICE_ID_MOTO_G = "4568735862378523675427897";
#Override
public void onActivityCreated(Bundle bundle)
{
super.onActivityCreated(bundle);
snippetContent = (LinearLayout)getView().findViewById(R.id.snippet_content);
adView = (AdView)getView().findViewById(R.id.adView);
adView.setAdListener(new AdListener()
{
#Override
public void onAdLoaded()
{
super.onAdLoaded();
if (context != null)
{
onAdFinishedLoadingListener = (OnAdFinishedLoadingListener)getActivity();
onAdFinishedLoadingListener.onAdLoaded();
}
}
});
adRequest = new AdRequest.Builder().addTestDevice(DEVICE_ID_MOTO_G).addTestDevice(DEVICE_ID_ZTE).build();
adView.loadAd(adRequest);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_snippet, container, false);
}
#Override
public void onPause()
{
if (adView != null)
{
adView.pause();
}
super.onPause();
}
#Override
public void onResume()
{
super.onResume();
if (adView != null)
{
adView.resume();
}
}
#Override
public void onDestroy()
{
destroyAdView();
super.onDestroy();
}
private void destroyAdView()
{
if (adView != null)
{
adRequest = null;
adView.removeAllViews();
adView.setAdListener(null);
adView.destroy();
snippetContent.removeView(adView);
adView = null;
snippetContent = null;
}
}
}
I was able to put together the code from Libgdx and Google play to add Ads from adMob but I cannot seem to bring the banner on top of the game screen and the banner only shows once I hit the back bottom and exit the app.
My code is below
any help would be appreciated
Android main class
public class MainActivity extends AndroidApplication {
static AdView adView;
private static final String AD_UNIT_ID = "my id";
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
//cfg.useGL20 = false;
//initialize(new GameActivity(), false);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
View gameView = initializeForView(new GameActivity(), true);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("my device")
.build();
adView = new AdView(this);
adView.setAdSize(AdSize.FULL_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.loadAd(adRequest);
RelativeLayout.LayoutParams gameParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
gameParams.bottomMargin = 1;
layout.addView(gameView, gameParams);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(adView, adParams);
setContentView(layout);
}
#Override
protected void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
#Override
protected void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
#Override
protected void onResume() {
if (adView != null) {
adView.resume();
}
super.onResume();
}
}
Solved see Edited code, added LayoutParams to the game view
I found it:
I just had to change
adView.setAdSize(AdSize.FULL_BANNER);
to
adView.setAdSize(AdSize.BANNER);
the issue is: the ad doesn't show on first request, but when it makes the second request, it shows right. About 2 seconds before it make the second request, the ad of the first request shows up. Any ideas?
Thanks,
EDIT: I changed the gravity to TOP, and now it shows on the first time, but is showing just the top half of the ad, bizarre, any ideas?
#Override
protected void onSetContentView() {
if(adView != null){
return;
}
final FrameLayout frameLayout = new FrameLayout(this);
final FrameLayout.LayoutParams frameLayoutLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
final FrameLayout.LayoutParams adViewLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(getResources().getString(R.string.ad_unit_id));
adView.setAdListener(new ToastAdListener(this));
adView.loadAd(new AdRequest.Builder().build());
this.mRenderSurfaceView = new RenderSurfaceView(this);
mRenderSurfaceView.setRenderer(mEngine,this);
final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
frameLayout.addView(adView, adViewLayoutParams);
this.setContentView(frameLayout, frameLayoutLayoutParams);
}
#Override
protected void onPause() {
if(adView!=null){
adView.pause();
}
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
if(adView!=null){
adView.resume();
}
}
#Override
protected void onDestroy() {
if(adView!=null){
adView.destroy();
}
super.onDestroy();
}
The problem was solved for me by adding the line
adView.setBackgroundColor(android.graphics.Color.TRANSPARENT);
The way I have done is as follows:
In the method which changes the visibility, I first check is the ad was loaded. If it was not, I don't change the visibility
Set an AdListener for the AdView. in the onReceivedAd(), I check the condition to hide it - if it should be hidden, I hide.
Works fine this way.
I have got the same problem, I solved this way:
Advertisement class:
public class Advertisement {
private AdView adView;
private final Handler adsHandler = new Handler();
public Advertisement(final Activity activity) {
adView = (AdView)activity.findViewById(R.id.adView);
}
//show the ads.
private void showAds () {
adView.loadAd(new AdRequest.Builder().build());
adView.setVisibility(android.view.View.VISIBLE);
adView.setEnabled(true);
}
//hide ads.
private void unshowAds () {
adView.loadAd(new AdRequest.Builder().build());
adView.setVisibility(android.view.View.INVISIBLE);
adView.setEnabled(false);
}
final Runnable unshowAdsRunnable = new Runnable() {
public void run() {
unshowAds();
}
};
final Runnable showAdsRunnable = new Runnable() {
public void run() {
showAds();
}
};
public void showAdvertisement() {
adsHandler.post(showAdsRunnable);
}
public void hideAdvertisement() {
adsHandler.post(unshowAdsRunnable);
}
}
In code:
...
public static Advertisement advertisement = new Advertisement(this);
...
public static void showAd()
{
if (advertisement != null)
advertisement.showAdvertisement();
}
public static void hideAd()
{
if (gangsterAdvertisement != null)
advertisement.hideAdvertisement();
}
I new in AdMob. At first I'm trying to create a test app with different types of ad.
I use real ad_unit_id and nexus 4 for testing.
I use 1 layout xml for all activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/empty_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
When i create simple activity with banner ad it's work fine. Activity code:
public class AdBannerActivity extends Activity {
private AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_empty);
adView = new AdView(this, AdSize.SMART_BANNER, getString(R.string.adUnitId));
adView.setAdListener(this);
LinearLayout layout = (LinearLayout) findViewById(R.id.empty_layout);
layout.addView(adView);
adView.loadAd(new AdRequest());
}
#Override
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
When i try to create activity with full screen ad, i have error message:
Ad request successful, but no ad returned due to lack of ad inventory.
Activity code:
public class AdInterstitialActivity extends Activity implements AdListener {
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_empty);
interstitial = new InterstitialAd(this, getString(R.string.adUnitId));
interstitial.setAdListener(this);
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onReceiveAd(Ad ad) {
if (ad == interstitial) {
interstitial.show();
}
}
#Override
public void onDismissScreen(Ad ad) {
}
#Override
public void onFailedToReceiveAd(Ad ad, ErrorCode error) {
Log.e("AdTest", "Error code: " + error);
}
#Override
public void onLeaveApplication(Ad ad) {
}
#Override
public void onPresentScreen(Ad ad) {
}
}
What am I doing wrong?
How to solve this problem?
Solution: Create new ad_unit_id for interstitial ads in AdMob account.
If your have that error, then your code is correct. Admob just doesn't have any ads to display for your app (your country, your type of ads are determinants).
If you want to test the behavior of your ads in test mode, you should take a look at this link :
https://developers.google.com/mobile-ads-sdk/docs/admob/additional-controls#testmode
EDIT : If you have just created your Admob account, it may take some times and some requests to deliver the first ads.
I saw somewhere that people had this error too by using custom size for banner, so be sure to check :
https://developers.google.com/mobile-ads-sdk/docs/admob/intermediate#play