For example, on one of my app screens where you have a Banner ad, you have the following code in the xml layout:
<com.google.android.gms.ads.AdView
android:id="#+id/adViewPrincipal"
android:layout_width="0dp"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/adId" />
And also in java code has the following code:
MobileAds.initialize(this, String.valueOf(R.string.adId));
adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
It seems that everything is working with this test id, but when I take the ID parameter in this java code, leaving:
MobileAds.initialize(this);
or
MobileAds.initialize(this, "");
The ad keeps running without any errors in the app, so do I just need to set the ad id in XML? Is that in all the tutorials I've seen people putting the ID also in java code. If I leave the parameter with nothing, nothing goes wrong, it's like this parameter is useless, and when I get the ID from the XML code, that crashes app. Can I put the ad id only in XML code then?
You are passing the second argument of initialize wrong. It is supposed to be app-id of admob on ad-unit id. The ad id is used normally only in xml views, where app_id is used in code to initialize ads for the first time. Read More here
MobileAds.initialize(this, getResources().getString(R.string.app_id));
Can you try everything programmatically:-
LinearLayout adContainer = <container>;
AdView adView = new AdView(activity)
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(<your-publisher-id>); // you can use id from constants
// Initiate a generic request to load it with an ad
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(adRequest);
// Place the ad view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
adContainer.addView(adView, params);
Related
I am trying to implement a banner advert in my Android application. I have used Gradle to download the dependencies, etc.
In my activity where I want the advert to be displayed I have:
<com.google.android.gms.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_mob_id" />
And ad_mob_id is
ca-app-pub-3940256099942544/6300978111
Which is for test ads, I took that from https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start#step_1_modify_the_main_activity_layout
However when I run the activity nothing is displayed and there are no errors/warnings generated.
Am I missing another step?
AdView mAdView = (AdView) getView().findViewById(R.id.adView); // locate the id the banner view
// if you do not use fragments and you are using it directing the remove the getView();
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest); // load it..that's all and you will see logs on it
i took it from the link you posted, it has all you need.
I want to track clicks on an AdMob banners with Google Analytics, but a problem occurs and I don't understand why.
Currently, my AdMob banner is implemented like this:
Layout:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="YOUR_AD_UNIT_ID"
ads:adSize="BANNER"/>
Java : AdView adView = (AdView)this.findViewById(R.id.adView);
However, the Google demonstration project which shows how to add an AdListener (project available here) does not specify anything in the layout and use the following code to add the banner:
LinearLayout layout = (LinearLayout) findViewById(R.id.leLinearLayoutDeMonChoix);
layout.addView(adView);
But if the implementation described at the beginning is used, the AdListener does not detect any event anymore. Why?
You can find this defective implementation in the following demonstration project: https://drive.google.com/file/d/0B8rE1pbtzNJ1UXg5QllubEFidGc/edit?usp=sharing
I thank you in advance for your time and help.
In the provided implementation you are doing the following:
// Create an ad.
adView = new AdView(this);
// Set the AdListener.
adView.setAdListener(new AdListener() {
/** stuff **/
}
AdView adView = (AdView)this.findViewById(R.id.adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
There is two instances of AdView, and this is your problem.
first one created from inflating your main.xml layout by setContentView(),
second one when you wrote adView = new AdView(this);
You set your listener on the second one, but only the first one is displayed.
It cannot work. :)
Choose one method (create it from layout) or the other (create it from code), but don't mix them up.
If you want to create your ad from layout, do the following:
// Retreive the adView.
AdView adView = (AdView)this.findViewById(R.id.adView);
// Set the AdListener.
adView.setAdListener(new AdListener() {
/** stuff **/
}
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
Hi I am using AdMoB for showing banner ad in YouTube app which retrieves Video From Channel . For showing ad I used Java Code rather than XML. Now I am facing one problem which is my Banner is shown behind the Videos after the gets loaded. Can someone please help me to find out a way to show the ad front of the Videos. This is the code i used
adView = new AdView(this, AdSize.BANNER, "caXXXXXXXXXXXX");
FrameLayout layout = (FrameLayout)findViewById(R.id.content_frame);
layout.addView(adView);
#SuppressWarnings("deprecation")
FrameLayout.LayoutParams adsParams = new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
adView.loadAd(new AdRequest());
Try this way.
This is the xml code of the banner, you can place it wherever you want in your activity xml layout:
<com.google.ads.AdView android:id="#+id/banner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="YOUR UNIT ID HERE"
ads:adSize="BANNER"
ads:loadAdsOnCreate="true"/>
Then inside <application> tag in AndroidManifest.xml declare the AdActivity:
<activity
android:name="com.google.ads.AdActivity"
android:label="#string/app_name" >
And finnaly get the reference on your activity of banner and start request ads from google:
AdView ad = (AdView) findViewById(R.id.banner);
ad.loadAd(new AdRequest());
What layout are you using? To get ads on top of my Libgdx projects, I had to use a RelativeLayout and specify put things together in a specific order. Here's an example from my code:
// Create the main/content layout
RelativeLayout layout = new RelativeLayout(this);
// This is just a View that LibGDX draws into, it could be any View.
View gameView = initializeForView(new SuperAwesomeGameIMade(AndroidLauncher.this));
// Add it to the new content layout.
layout.addView(gameView);
// Create and setup the AdMob View
mAdView = new AdView(AndroidLauncher.this);
mAdView.setAdSize(AdSize.SMART_BANNER);
mAdView.setAdUnitId("your ad unit here");
// Create the ad load request using the AdRequest Builder
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
adRequestBuilder.addTestDevice("your test device ID so you don't get banned!");
mAdView.setVisibility(View.GONE); // Change to View.Visible to show ads
mAdView.loadAd(adRequestBuilder.build());
// Setup the AdView with new layout params so it can "float" on the other one.
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_RIGHT);
layout.addView(mAdView, adParams);
// Set the content view as this new relative layout
setContentView(layout);
Hope that gets you pointed in the right direction.
I have tried everything, applied every solution I found. Checked googleplayservices many times. Still admob is not working.here I see no need to worry.
Does that mean if I submit my app, admob will work fine after submitting? If not here is my code for admob,how do I fix this?
private AdView adView;
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("My App Ad Unit ID");
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.adViewLayout);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// .addTestDevice("52EEC3DAD2FA75F22B44407D19072632")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
XML:
<LinearLayout
android:id="#+id/adViewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
</LinearLayout>
This is what the google guys say over it
I keep getting the error 'The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.'
You can safely ignore this message. Your app will still fetch and serve banner ads.
Also this has bee asked several times so just ignore it
I want to add the Admob / AdWhirl into the GameLayer Scene.
I search over everywhere but couldn't find the way to do this work. I don't want to switch the Library. So , what should i do?
If someone have worked on it , give some way to do this .
as there is not layout xml file for cocos2d android you can add it progammatically. crate linear layout in onstart method itself.
like this
LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
getWindowManager().getDefaultDisplay().getWidth(),
getWindowManager().getDefaultDisplay().getHeight()+getWindowManager().getDefaultDisplay().getHeight()-50);
adView = new AdView(SimpleGame.this, AdSize.BANNER, "your Ad ID");
adView.setAdListener(SimpleGame.this);
AdRequest request = new AdRequest();
request.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(request);
CCDirector.sharedDirector().getActivity().addContentView(adView,adParams);
this should be in try and catch.
of course we can add admob and or any other lib in your cocos2d games activity class.
You need to implement adlistener to your activity and add the adview in the OnStart() method of your game .