Cannot get AdMobs to display over map - android

I have followed this tutorial: AdMob Code
But I cannot get the banner to display (not even the common XML adSize error that I have seen a lot of online:
This is the code in my main_activity:
private AdView adView;
private static final String AD_UNIT_ID = "ca-app-pub-XXXXXXXXXXXXXXXXXX";
GoogleMap mMap;
LatLng myposition;
Marker marker;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create an ad.
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(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.lyout);
layout.addView(adView);
//Create an ad request. Check kogcat 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("MY_DEVICE_ID")
.build();
//Start loading the ad in the background.
adView.loadAd(adRequest);
}
And this is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lyout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTilt="45"
map:cameraZoom="15"
/>
</LinearLayout>
At first my layout file was only:
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTilt="45"
map:cameraZoom="15"
/>
EDIT: LogCat removed as working
But that wasn't working either. How do I get these ads to display?
Thanks

You can load the banner ad like below from the xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:testDevices="TEST_EMULATOR"
ads:loadAdOnCreate="true"
ads:adSize="BANNER"/>
<fragment xmlns:map="http://schemas.android.com/apk/res-auto"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/the_map"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:cameraTilt="45"
map:cameraZoom="15" />
</LinearLayout>
Note: if you have generated your appId just recently in admob account then may be there will be no advertise to display for your id to Admob engine so wait for a while and then try again. May be that will also work.
Hope it will help you..

Related

Admob banner visible in preview mode, but not when running

Im trying to add a banner to my Android application, but can't get it to work. The banner is visible in the preview but not when running on my Genymotion device. This is my layout-file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.my.package.MapsActivity"
tools:layout="#android:layout/list_content"
android:orientation="vertical">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="450dip"
tools:context="com.example.my.package.MapsActivity"/>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
ads:adSize="BANNER"
ads:adUnitId="#string/banner_ad_unit_id"/>
</LinearLayout>
And in the Activity-file I have this code:
MobileAds.initialize(mContext, "ca-app-pub-xxxxxxxxxxxxxxxxx~xxxxxxxxxx");
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
It does not work with test ads either. What can be wrong?
Hank
One thing I would like to say that whenever you create a banner or interstitial than you have to wait for few hours for ads to display on your mobile screen.
Maybe your gmail account used in the Genymotion is the same with admob account. Try change to another gmail in your Genymotion.

Adjust the Layout when AdMob Banner get Displayed

I am integrating AdMob Banner ad in my Activity. The layout consist of a GridView and AdView.
What I Want:-
When ad is not shown, i.e. at start or when ad is getting downloaded or anything, I want the GridView to have all the screen. But when ad is to be displayed, it should cover required space below but do not overlap anything.
My Layout:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
android:id="#+id/ll_gridmain"
tools:context="mohits.app.picreddit.itemgrid">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
android:divider="#null"
android:numColumns="2"
android:background="#454545" >
</GridView>
<LinearLayout android:id="#+id/banner_view"
android:orientation="horizontal"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:visibility="gone">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_banner"
android:visibility="gone">
</com.google.android.gms.ads.AdView>
</LinearLayout>
</LinearLayout>
My Activity:-
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("xxx")
.build();
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
mAdView.setVisibility(View.VISIBLE);
bannerView.setVisibility(View.VISIBLE);
}
});
What I am Getting:-
The banner_view layout is always showing even if I am setting it to GONE. This should only become visible when the ad is to be shown.
Thank you.
The problem is even if you remove your banner, the weight of the GridView will still be 9 and the weightSum of the layout is 10, so the GridView will not fill the whole LinearLayout.
Removing the following line from your layout should work:
android:weightSum="10"
Also, setting the visibility of the AdView is redundant.
Setting the visibility of a ViewGroup will also affect their children.
You could remove the following attribute from your AdView:
android:visibility="gone"
After that, setting the visibility of banner_view should be enough:
#Override
public void onAdLoaded() {
super.onAdLoaded();
bannerView.setVisibility(View.VISIBLE);
}

Admob not showing on tablet using fragments No Enough Space

I'm using fragments in an Android app, in the Fragment class I'm calling admob in onViewCreated, the Admob banner shows without any problem in Smartphone when calling only one fragment, but not showing at all in the tablets when calling 2 fragments.
EDIT:
Just found in the logcat :
04-07 15:35:37.373: W/Ads(1380): Not enough space to show ad. Needs 1024x50 dp, but only has 340x471 dp.
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// TODO: load ads
mAdView = (AdView) getView().findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.build();
mAdView.loadAd(adRequest);
}
In the fragment XML layout file, I've added the admob AdView :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white" xmlns:app="http://schemas.android.com/apk/res-auto">
<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"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
app:adSize="SMART_BANNER"
app:adUnitId="#string/admob_banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
</RelativeLayout>
This is the layout file for sw720p calling 2 fragments:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/darker_gray" xmlns:tools="http://schemas.android.com/tools">
<fragment android:name="com.recipes.app.RecipesListFragment"
android:id="#+id/list_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="2dp"/>
<fragment android:name="com.recipes.app.DetailFragment"
android:id="#+id/detail_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"/>
</LinearLayout>

My app was suspended due to an issue with ads

today Google suspended my app saying that my ads violated Google TOS but I don't know what did it actually violate. I think there might be something wrong with my code structure i guess because I tried publishing it two times and in both it got suspended.
In my app I am using a View Pager and inside it I have 4 tabs. So the ads are placed in the View Pager as below
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<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"
app:adSize="SMART_BANNER"
app:adUnitId="#string/banner_ad_unit_id" >
</com.google.android.gms.ads.AdView>
and the code in the View Pager activity
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
I would be very thankful if someone told me the reason behind the continuous suspension.

Setup the bottom of the page AdMob banner

I'm trying to setup an AdMob banner on the bottom of the page, but the Ad does not show.
Space appears the bottom of the page there, but the AdMob does not show there.
Add and reference the Google Play services library. - YES
Add a meta-data tag in AndroidManifest.xml. - YES
Declare com.google.android.gms.ads.AdActivity in the manifest. - YES
Set up network permissions in the manifest. - YES
Somebody tell me why.
Thank you.
My XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/flContainer" <!-- DATA Layout -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/ad_layout"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/ad_layout" <!-- AdMob Layout -->
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" />
</RelativeLayout>
MainActivity.java
// Create an ad.
adView = new AdView(context);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(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.ad_layout);
//layout.getBottom();
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("INSERT_YOUR_HASHED_DEVICE_ID_HERE").build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
You told fldContainer to matchParent. THis means it will consume all the space of the parent, leaving none for your ad.
Instead try:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/flContainer" <!-- DATA Layout -->
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/ad_layout" <!-- AdMob Layout -->
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical" />
</LinearLayout>

Categories

Resources