Managing Admob publisher ID from a central place with multiple ad placements - android

So, with new google-admob SDK for android correct way to place banner ad in the layout is for example like this :
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_gravity="bottom"
android:layout_height="wrap_content"
ads:adUnitId="some id"
ads:adSize="BANNER"/>
My problem is that I have multiple banner ads throughout various activities and when I for some reason want to change adUnitId I do not want to chase this snippet of code through all my xml layout files. I want to have one place where I define my adUnitId that all banner ads in the app uses. That was possible in the previous admob-only SDK, by setting meta data in manifest file. And I do not want to layout AdView in Java code, want it to be in xml if possible with the new SDK.
I tried with something like this :
ads:adUnitId="#string/admob_id"
but it just see it as a string as I can see, not following through to the actual string defined in the strings.xml. Any ideas?

With the newest admob update, I was forced to do most of my AdMob activities in my Java classes. This is how I made it work (with a centralized ID):
ad = new AdView(this, AdSize.BANNER, UtilClass.AD_PUBLISHER_ID); //Ad is the global AdView
LinearLayout layout = (LinearLayout) findViewById(R.id.main_admob_layout);
Edit: I keep the LinearLayout definition in the xml files so the positioning is all done throughout the xml:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="bottom" android:weightSum="0"
android:layout_alignParentBottom="true" android:id="#+id/main_admob_layout">

Related

What is the appropriate value for adSize in the xml file if I want to include an InterstitialAd and not a Banner?

What is the appropriate value for adSize in the xml file if I want to include an InterstitialAd and not a Banner?
My problem is that including an InterstitialAd from Admin to my application does not work properly.
So far I followed the official tutorial, but it says nothing about the value of ads:adSize in the xml. When I implemented a ad with a banner this attribute was set with "BANNER" but I suppose this is not the solution when I want to have an Interstitial ad.
So could you please tell me which other values than BANNER are possible for ads:adSize or what else I should use to make it work?
Thanks a lot
Below code required only when you want to add banner view at your App view.
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
Follow instruction of this link to implement InterstitialAd in your App.

what to put as adunitid in xml layout for android

In admob management AdUnit shows in format
ca-app-pub-xxxxxxxxxxxxxxxx/nnnnnnnnnn
How am i suppose to put it in xml layout?
Is this correct?
<com.google.android.gms.ads.AdView
android:id="#+id/ad1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxxx/nnnnnnnnnn"
/>
You already have the correct Ad unitId..
It should be something like "ca-app-pub-xxxxxxxxxxxxxxxx/nnnnnnnnnn"

Admob not affected by activities?

i have created a game(surfaceview) and i am trying to implement admob wherein it must be fixed at the bottom.. i have successfully implemented the admob and shows it properly but what i did is i call it in main activity.
AdView adView = (AdView)findViewById(R.id.adView);
adView.loadAd(new AdRequest());
and i have this on all layouts
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/unit_id"
ads:loadAdOnCreate="true"
/>
what i need to do is show the admob but it will not be affected even if the screen switches it is fixed in the bottom part of the screen. i have searched for some thing like this but i have no success. i hope someone can help or even explain on how i can achieve this.
P.S. its like 1 banner for all activities.
I think it will be difficult (or impossible) to have just a single unique banner span multiple Activities. The ActivityGroup group class may have allowed this kind of thing, but it was never popular and anyway has been deprecated for a while.
Meantime there remains plenty of demand for the UX motif of switching between major views within the confines of one screen. But the mechanism has been reworked to use Fragments. If you can make your SurfaceView stuff work within a Fragment, then that might be your best bet. A layout might look something like this:
<LinearLayout>
<Fragment>
<AdView>
<LinearLayout>
When I did this kind of thing recently, I just put the AdMob code at the bottom of every Activity, and let it reload a fresh ad with every push. There has been no scolding from the Google side regarding too-frequent ad requests, and I am getting plenty of clicks.
Good luck!
Use Only Xml Coding without Java Code
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="App ID"`enter code here`
ads:testDevices="25"
ads:loadAdOnCreate="true" />
</RelativeLayout>
Can you wrap the whole screen in a relative layout? You can then align the banner to the parent's bottom. You other items should be declared with layout_above to keep them above the banner. The easiest way is to wrap them in another layout.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/adView" >
... your other items can go here
</RelativeLayout>
<com.google.ads.AdView
android:id="#id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="App ID"`enter code here`
ads:testDevices="25"
ads:loadAdOnCreate="true" />
</RelativeLayout>

List of AdMob attributes used in xml and in extras

Searching through the web didn't give me the reference to neither the attributes which may be used when setting up AdMob's AdView in xml, nor the valid values used as AdRequest.extras.
The official documentation on AdView in XML gives just following example:
<com.google.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
While the guide on extras specifies just following values:
color_bg Background color
color_bg_top Gradient background color at top
color_border Border color
color_link Link text color
color_text Text color
color_url URL color
So, are these values the only possible way to customize (not considering Targeting) the AdView and AdRequest or are there any unmentioned values/attributes?
The AdView also has an ads:keywords attribute which takes in a comma separated list of keywords. AdMob doesn't use the keywords at all though - it just passes them to AdSense backfill if you have that enabled.
I'm not aware of any other extras that AdMob itself uses. For those who are using DoubleClick for Publishers with AdMob, you can use the extras for custom targetting of your ad inventory.

Android/Admob: Can I set a list of testing devices in the Layout XML for the AdView?

So, I currently have an AdView in my layout like this:
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="XXXXXXXXXXXXXX"
ads:adSize="BANNER"
ads:loadAdOnCreate="false"/>
and that works just fine. However, I have to get the AdView in the source code in order to pass it a request for the Emulator and my personal phone to be TestingDevices and not receive real ads. Is there a way I can list those in the XML directly? or even force the AdView to ONLY show test ads, so that I don't have to have that chunk of code and can set the AdView to loadAdOnCreate?
Thanks.
Sure, add this to the xml:
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
I found it here: https://developers.google.com/mobile-ads-sdk/docs/android/banner_xml

Categories

Resources