I'm trying to implement AdMob into an Android App. The app has 1 activity(MainActivity) and in this activity I have 10 fragments. I would like to display a banner ad in each fragment. Can I use the same ad unit for all the fragments or it will be necessary to create an ad unit for each one?
You can use one Banner Ad Id for one Application. Just create string in string.xml and use the same in application. Like this -
strings.xml
<string name="ad_banner">ca-app-pub-903891699798xxxx/244573xxxx</string>
layout.xml
<com.google.android.gms.ads.AdView
android:id="#+id/ad_view"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginTop="1dp"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_banner" />
Related
I use AdViews in my app in almost every activity.
The xml im using is:
<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="50dp"
android:layout_centerInParent="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/Banner_Ads"
ads:layout_constraintEnd_toEndOf="parent"
ads:layout_constraintStart_toStartOf="parent"
ads:layout_constraintTop_toTopOf="parent"
app:gnt_template_type="#layout/gnt_small_template_view" />
I did every thing I could to test it by either using Test ID for the ads or by setting TestDeviceID and everything works good.
How can I be sure that it will work with real ads? when i did a single check of my app before distribution it gives me ad failed to load : 3.
Yes, I read that it can happen if I created ad unit recently and that it might might take time or that it means that my code is correct but there are no ads. But what does it means no ads? isnt AdView meant to be for ads?
Thank you
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.
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"
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>
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">