I am trying to set the AdMob banner ABOVE the Tabs in a TabsActivity:
public class FundsReportTabsActivity extends TabsActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdView adView = new AdView(activity, AdSize.BANNER, AD_UNIT_ID);
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="#+id/mainLayout"
// Add the adView to it
getTabHost().addView(adView, 0, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice("E0154BC3F11C778234254195FA1164F");
adRequest.setTesting(true);
// Initiate a generic request to load it with an ad
adView.loadAd(adRequest);
}
However, the banner does not appear, even though I have this logcat:
09-29 22:23:09.755: INFO/Ads(16158): onReceiveAd()
and don't see any related warning/error.
Does anyone have an idea? Thanks!
Check your logcat for a warning or verbose saying no ads in inventory. Some times when you first sign up or are using admob it takes some time before the ads start appearing. Also make sure you have provided a linear layout for the ad. And if you watch the logcat file during the activity with the ad you will see a content containing items that pertain to the ad. I know i had trouble with admob at first. The same issue you are having. It just took some time.
Related
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);
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);
I recently migrated my project from AdMob SDK to Google service Ads, however I got a tiny problem with the Google service Ads.
I have a banner ad view in the activity, and I would dynamically adjust the buttons in the layout depending on whether or not an ad is loaded. It worked fine when I used AdMob SDK, but now with Google service Ads, the banner is always reserved there with blank before the ad is loaded. And if the ad cannot be loaded (say without network), the blank view is there, which is rather ugly! This is also why I would like to adjust the layout dynamically...
Did I missed anything while I changed the code? Thank you for help!
Here is an excerpt of the java code and layout file:
Java:
import com.google.android.gms.ads.*;
...
public class MyActivity extends Activity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
...
AdView adView = (AdView)findViewById(R.id.ad);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
Layout XML:
...
<com.google.android.gms.ads.AdView
android:id="#+id/ad"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="AD_PUB_ID"
ads:adSize="BANNER"/>
</RelativeLayout>
You can add an AdListener, and make the ad visible only when it receives an ad.
The code looks like this:
final AdView ad = new AdView(context);
ad.setAdUnitId(publisherId);
ad.setAdSize(AdSize.SMART_BANNER);
final AdListener listener = new AdListener() {
#Override
public void onAdLoaded() {
ad.setVisibility(View.VISIBLE);
super.onAdLoaded();
}
};
ad.setAdListener(listener);
ad.setVisibility(View.GONE);
adParent.addView(ad);
ad.loadAd(new AdRequest.Builder().build());
Since there is/was an issue, that an AdMob ad generates an ANR, when it is destroyed when still loading. I normally also check on the visibility of the ad, before calling pause() or destroy().
Be careful because this is against Google Ad policy to have the ad banner pops up like that.
I received a mail from Google a month ago and had to change that behavior to avoid my app to be banned from the Play Store.
A lot of developers are receiving the same mail.
I have successfully gotten AdMob test ads to show up in a LinearLayout, but if I change to a TableLayout, they do not show up. No errors show in logcat.
This is what works:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdRequest request = new AdRequest();
request.setTesting(true);
request.addTestDevice(AdRequest.TEST_EMULATOR);
AdView adview = new AdView(this, AdSize.BANNER, [admob id deleted for posting on stackoverflow]);
// all code before this point is the same in both examples
LinearLayout layout = new LinearLayout(this);
layout.addView(adview);
// all code after this point is the same in both examples
this.setContentView(layout);
adview.loadAd(request);
}
I get a nice little banner ad at the top of the emulator screen that says "Success! Now you are ready to travel through the App Galaxy [Google logo]". So far so good. But it does not appear if I change it to a TableLayout, as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdRequest request = new AdRequest();
request.setTesting(true);
request.addTestDevice(AdRequest.TEST_EMULATOR);
AdView adview = new AdView(this, AdSize.BANNER, [admob id deleted for posting on stackoverflow]);
// all code before this point is the same in both examples
TableLayout layout = new TableLayout(this);
TableRow row = new TableRow(this);
row.addView(adview);
layout.addView(row);
// all code after this point is the same in both examples
this.setContentView(layout);
adview.loadAd(request);
}
I get no errors in LogCat. In both instances (LinearLayout or TableLayout) all I get in LogCat from Admob is two informational messages:
Ads Received ad url: <"url": "[URL to the 'App Galaxy' test ad]">
Ads onRecievedAd()
That's it. No complaints about not having enough space, which seems common from other posts. All I get is a black screen. If I add in another TableRow afterward, containing a TextView, that TextView shows up 50px below the top of the screen, as if the ad were there.
Any ideas on stuff to try to get it to show up? I've already written an app based on a TableLayout and if possible I'd rather not have to redo it in LinearLayout(s) . . .
I guess the parent of the AdView should have the property:
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
I've tried to make a test app to see how admob works. But almost every time "No ad to show". Here is my source code:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView adView = new AdView(this, AdSize.BANNER, "xxxxxxxxxxxxxxxxx");
adView.setAdListener(this);
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
layout.addView(adView);
adView.loadAd(new AdRequest());
}
but in a hour, it appeared only once. Am I doing something wrong?
It works fine but you should wait for a moment at the first time , if you want just to test you can use the test mode by adding this line (new AdRequest()).setTesting(true);
Make sure that you are using the general key and not the debug key for the AdMob API