I have made a free android game and I wanted to add a banner to it. I followed this tutorial and I don't know why it won't work.
When I run my game on the emulator in LogCat I see a message "Ad is not visible. Not refreshing ad." and when I install the game on my phone I also don't see any ads.
Here is my main.xml
<?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="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="hidden"
ads:adSize="BANNER"/>
</LinearLayout>
and here is the code in MainActivity.java
public class MainActivity extends AndroidApplication {
private AdView adView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = true;
cfg.useAccelerometer = true;
cfg.useCompass = true;
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
initialize (new RedSquare(), cfg);
}
@Override
public void onPause() {
adView.pause();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
adView.resume();
}
@Override
public void onDestroy() {
adView.destroy();
super.onDestroy();
}
}
There is nothing especially wrong with your layout. Though I would probably change the layout_width for AdView to be:
But it is not clear what
is doing? I suspect it is altering the layout.
I've found out using
adMob
defining the layout explicitely in a file could be a huge headache generator, as sometimes it might turn impredictable with some parameter and layout combination. What always seems to work is adding theAdView
dynamically to someLinearLayout
.This should work:
---- EDIT ----
This implementation is for
Fragment
s, you probably are running anActivity
and that's why you're getting those errors.Replace
getActivity()
withthis
The
inflater
simply (as it name says) inflates a newLinearLayout
from a layout file. This means I have defined a layout which I've calledyour_layout
and defined inside aLinearLayout
. The inflater just creates an instance of that `LinearLayout. In your case probably it's not necessary.I'm adding a code that probably will work for you:
To show ads in libgdx you need to initialize your app for view. So, replace all this:
With this: