i have problem with admob. Admob is working good but when i exit my game admob is still working. What i done wrong ? Please help me.
Even if i exit the game, in the logcat i see that admob is still working.
AndroidLuncher.java
public class AndroidLauncher extends AndroidApplication {
private AdView adView;
private static final String AD_UNIT_ID = "xxxxxxxxxxxxxxxxxxxxxxx";
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useWakelock = true;
cfg.useAccelerometer = false;
cfg.useCompass = false;
// Create the layout
RelativeLayout layout = new RelativeLayout(this);
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libgdx View
View gameView = initializeForView(new MyGameStart(), cfg);
// Add the libGDX view
layout.addView(gameView);
// Create and setup the AdMob view
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.build();
// Add the AdMob view
RelativeLayout.LayoutParams adParams =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_IN_PARENT);
adView.loadAd(adRequest);
layout.addView(adView, adParams);
// Hook it all up
setContentView(layout);
}
#Override
public void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
#Override
public void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
#Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
} }
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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="xxxxxxxxxxxxxxxxxxxxxx"
ads:adSize="SMART_BANNER"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pl.com.test.testgame.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" />
<!-- Include required permissions for Google Mobile Ads to run-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<!--This meta-data tag is required to use Google Play Services.-->
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="pl.com.test.testgame.android.AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
</manifest>
02-13 14:58:55.271: I/Ads(2604): Starting ad request.
02-13 14:58:56.111: I/Ads(2604): Scheduling ad refresh 60000 milliseconds from now.
02-13 14:58:56.111: I/Ads(2604): Ad finished loading.
EXIT GAME
02-13 14:59:56.121: I/Ads(2604): Starting ad request.
02-13 14:59:56.931: I/Ads(2604): Scheduling ad refresh 60000 milliseconds from now.
02-13 14:59:56.931: I/Ads(2604): Ad finished loading.
Create an onStop() method and stop adMob there as well.
Try setting the AdView instance to NULL and leave it there for the GC to handle it. It probably will trow a NullPointerException so make sure you handle that.
I solved the problem.
I had two times Adview adview;
public class AndroidLauncher extends AndroidApplication {
private AdView adView;
#Override
protected void onCreate (Bundle savedInstanceState) {
***
// Create and setup the AdMob view
AdView adView = new AdView(this);
***
When I removed one, AdMob is working correctly.
Related
So in my LIBGDX game I'm trying to impement Admob ads.
They load fine but just wont show, even after a 60 second refresh or pressing home button and entering the game again.
I followed some answers on this site, but none of them helped.
Here's the code of Android Launcher class:
public class AndroidLauncher extends AndroidApplication implements AdHander{
private static final String TAG = "AndroidLauncher";
protected AdView adView;
private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;
Handler handler = new Handler(){
#Override
public void handleMessage(Message msg) {
switch (msg.what){
case SHOW_ADS:
adView.setVisibility(View.VISIBLE);
break;
case HIDE_ADS:
adView.setVisibility(View.GONE);
break;
}
}
};
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout layout = new RelativeLayout(this);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
View gameView = initializeForView(new Main(this), config);
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
// HERE I IMPLEMENTED ADMOB TEST ID FOR BANNERS
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
adView.setBackgroundColor(Color.BLACK);
adView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
int visibility = adView.getVisibility();
adView.setVisibility(AdView.GONE);
adView.setVisibility(visibility);
Log.i(TAG, "Ad loaded");
}
});
AdRequest.Builder builder = new AdRequest.Builder();
adView.loadAd(builder.build());
layout.addView(gameView);
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
setContentView(layout);
config.useImmersiveMode = true;
initialize(new Main(this), config);
}
#Override
public void showAds(boolean show) {
handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}
}
What have I did wrong?
Check your manifest it should look similar to this one :-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.serveroverload.recorder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<!-- Include required permissions for Google Mobile Ads to run -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<!-- This meta-data tag is required to use Google Play Services. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="com.serveroverload.recorder.ui.HomeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
</manifest>
#Override
public void showAds(boolean show) {
Message msg = new Message();
msg.what = show ? SHOW_ADS : HIDE_ADS;
handler.sendMessage(msg);
}
I have an unpublished Android app that I have inserted interstitial ads into using the AdMob Interstitial Guide and the ads did not initially show. So, thereafter I referenced Admob interstitial ad won't display and I added this:
interstitial.setAdListener(new AdListener(){
#Override
public void onAdLoaded(){
displayInterstitial();
}
});
Instead of just loading the add using displayInterstitial(). After doing this it still will not show the ads. Then I referenced "Because Admob interstitial ads won't display" and that did not help because I had already added .Builder().build() to the new AdRequest.
Afterwards I referenced "How to show interstitial ads?" and that was not useful because I am testing on a real device and not an emulator, and it also just repeated what the Google Interstitial Guide (first link) showed me how to do. Then I finally looked at Interstitial Ad's Listener's onAdLoaded() won't run and I added the Internet permission in my manifest (I had already added the meta-data property to my manifest as a child of application) and tried this code to show the ad:
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
interstitial.show();
}
});
And that did not work either.
I have 2 activities using ads (the app has a MainActivity, a GameScreen activity, and a Redirect activity)
Here is the MainActivity (I have excluded all the code except the ad involved code):
public class MainActivity extends ActionBarActivity {
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-3171635XXXXXXXXX/XXXXXXXXXX");
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
super.onAdLoaded();
interstitial.show();
}
});
}
// Invoke displayInterstitial() when you are ready to display an interstitial.
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
Here is the Redirect activity (I have excluded all the code except the ad involved code, identical to the MainActivity's ad showing code):
public class Redirect extends ActionBarActivity {
private InterstitialAd interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_redirect);
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("ca-app-pub-317163XXXXXXXXXX/XXXXXXXXXX");
// Create ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener(){
#Override
public void onAdLoaded(){
super.onAdLoaded();
interstitial.show();
}
});
}
// Invoke displayInterstitial() when you are ready to display an interstitial.
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
Here is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".UI.MainActivity"
android:label="#string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".UI.GameScreen"
android:label="#string/title_activity_game_screen"
android:screenOrientation="landscape"
android:parentActivityName=".UI.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="benyamephrem.tilt.UI.MainActivity" />
</activity>
<activity
android:name=".UI.Redirect"
android:label="#string/title_activity_redirect"
android:screenOrientation="landscape"
android:parentActivityName=".UI.GameScreen" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="benyamephrem.tilt.UI.GameScreen" />
</activity>
</application>
I have heavily researched this and I haven't come up with any solution that has worked. I feel like I'm missing some small detail but I can't currently see why this is not working.
Any help appreciated!
You are missing the following activity in your manifest:
<!--Include the AdActivity configChanges and theme. -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
I have implement admob in my application and ads sowing properly. Also a/c is created on Admob for getting admob unit id.
All working fine but no impression count and clicks count increased past 2 weeks. Even I clarify admob unit id, its correct. Any reason behind it, your help will be appreciate.
Also another thing is, my logcat showing me:
03-03 12:29:32.982 28406-28406/com.sudosaints.admobdemo I/Ads﹕ Starting ad request.
03-03 12:29:32.983 28406-28406/com.sudosaints.admobdemo I/Ads﹕ Use AdRequest.Builder.addTestDevice("6B039BBD4D66338A349104EF25FC47D6") to get test ads on this device.
03-03 12:29:34.123 28406-28406/com.sudosaints.admobdemo I/Ads﹕ Scheduling ad refresh 30000 milliseconds from now.
Why my logcat showing me like this above?
In my whole project there is no any connection with "addTestDevice("6B039BBD4D66338A349104EF25FC47D6")" this id.
Any reason behind such type of log. or It is default?
Below is my code.
MainActivity.class
public class MainActivity extends ActionBarActivity {
public InterstitialAd interstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interstitialAd = new InterstitialAd(MainActivity.this);
interstitialAd.setAdUnitId(getResources().getString(R.string.admob_unit_id));
AdView adView = (AdView) this.findViewById(R.id.adView);
/*AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("sdfsdf").build();*/
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
displayInterstitial();
}
});
}
public void displayInterstitial() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
activity_main.xml
<RelativeLayout
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="#string/admob_unit_id"/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz.xyz">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent"/>
</application>
</manifest>
i have wrote SDL game and ported it to andorid
and now I tried to integrate it with admob but i failed
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<!-- Replace org.libsdl.app with the identifier of your game below, e.g.
com.gamemaker.game
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.libsdl.zomibeshooter"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto">
<!-- Create a Java class extending SDLActivity and place it in a
directory under src matching the package, e.g.
src/com/gamemaker/game/MyGame.java
then replace "SDLActivity" with the name of your class (e.g. "MyGame")
in the XML below.
An example Java class can be found in README-android.txt
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:label="#string/app_name"
android:icon="#drawable/ic_launcher"
android:allowBackup="true"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:hardwareAccelerated="true" >
<activity android:name="mygame"
android:label="#string/app_name"
android:configChanges="keyboardHidden|orientation"
android:screenOrientation="landscape"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="#android:style/Theme.Translucent" />
</application>
<!-- Android 2.3.3 -->
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<!-- OpenGL ES 2.0 -->
<uses-feature android:glEsVersion="0x00020000" />
<!-- Allow writing to external storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxxxx" />
</LinearLayout>
my SDLActivity.java
// Setup
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.v("SDL", "onCreate():" + mSingleton);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Prepare the Interstitial Ad
interstitial = new InterstitialAd(SDLActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId("xxxxxxx");
//Locate the Banner Ad in activity_main.xml
AdView adView = (AdView) this.findViewById(R.id.adView);
// Request for Ads
AdRequest adRequest = new AdRequest.Builder()
// Add a test device to show Test Ads
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("CC5F2C72DF2B356BBF0DA198")
.build();
// Load ads into Banner Ads
adView.loadAd(adRequest);
// Load ads into Interstitial Ads
interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
// Call displayInterstitial() function
displayInterstitial();
}
});
SDLActivity.initialize();
// So we can call stuff from static callbacks
mSingleton = this;
// Set up the surface
mSurface = new SDLSurface(getApplication());
if(Build.VERSION.SDK_INT >= 12) {
mJoystickHandler = new SDLJoystickHandler_API12();
}
else {
mJoystickHandler = new SDLJoystickHandler();
}
mLayout = new AbsoluteLayout(this);
mLayout.addView(mSurface);
setContentView(mLayout); #########// if i comment this i can see the ad but the game is not show!
}
public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
Log.v("ad","displayed");
interstitial.show();
}
}
if i comment this line i can see the ad but not the game ! :
setContentView(mLayout);
So how i can show the ad from starting my game until it exit ?
I am using LibGDX 0.9.9 and Admob 6.4.1. I set up banner ads to appear in the top right corner of my app. However when I load up my app I can't see the banner. Although when I press where the banner should be it acts like normal and starts to appear. This happens both in testing mode and real mode. Here is my code:
public class MainActivity extends AndroidApplication {
private AdView adView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
cfg.useAccelerometer = false;
cfg.useCompass = false;
RelativeLayout layout = new RelativeLayout(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
// Create the libgdx View
View gameView = initializeForView(new Game(), false);
// Create and setup the AdMob view
adView = new AdView(this);
adView.setAdUnitId("SECRET ID");
adView.setAdSize(AdSize.BANNER);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // Emulator
.addTestDevice("MY DEVICE ID") // My Nexus test
.build();
adView.loadAd(adRequest);
layout.addView(gameView);
// Add the AdMob view
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
setContentView(layout);
}
public void onDestroy() {
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
and the Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.keep.hopping"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:icon="#drawable/jumper"
android:label="Keep Hopping" >
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version"/>
<activity
android:name="com.keep.hopping.MainActivity"
android:label="Keep Hopping"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>
Anyone have any ideas as to why this weird behaviour is happening?
In my case the problem was solved simply by setting the background of the adview:
...
layout.addView(adView, adParams);
adView.setBackgroundColor(getResources().getColor(android.R.color.transparent));
...
you can use any other color.
here it is the fix for integration of AdMob6.4.1 with libgdx, I put some lines of code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:paddingTop="1dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</RelativeLayout>
this is the main.xml
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.main);
MainActivity.mContext = getApplicationContext();
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useGL20 = false;
cfg.useAccelerometer = false;
cfg.useCompass = false;
RelativeLayout layout = (RelativeLayout)findViewById(R.id.mainLayout);
mBlockPuzzleInstance = new BlockPuzzle(this);
initialize(mBlockPuzzleInstance, false);
View gameView = initializeForView(mBlockPuzzleInstance, true);
adView = new AdView(this);
adView.setAdUnitId("a152ffba4abde11");
adView.setAdSize(AdSize.SMART_BANNER);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
layout.addView(gameView);
// Add the AdMob view
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView, adParams);
setContentView(layout);
}
the banner should load now.
Happy Coding!
Lunatikul's answer worked for me, but the important part for me was the padding:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:paddingTop="1dp"
... >
</RelativeLayout>
I actually implemented it programmatically like this:
layout.setPadding(0, 1, 0, 0);
Hope this helps!
Your problem could be that your 1st ad is hidden. If you waited 60 seconds and then your Adview appeared, you can fix it by only adding the AdView to the layout once your first ad loads by using an AdListener:
// Create and setup the AdMob view
AdRequest.Builder ad_builder = new AdRequest.Builder();
ad_builder.addTestDevice("XXXXXXXXXXXXX"); // my HTC phone
m_AdRequest = ad_builder.build();
m_AdView = new AdView(this);
m_AdView.setAdSize(AdSize.SMART_BANNER);
m_AdView.setAdUnitId("ZZZZZZZZZZZZ");
m_AdView.setVisibility(View.GONE);
m_AdView.setAdListener(new AdListener() {
private boolean _first = true;
#Override
public void onAdLoaded() {
if (_first) {
_first = false;
// Add the AdMob view when the first ad gets loaded, this makes sure the first ad gets displayed
RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); // places the ad at the bottom overlapping your game
layout.addView(m_AdView, adParams);
}
}
});
Hope this solves your headache!