|
@@ -7,14 +7,18 @@ using Android.Views;
|
|
using Android.Widget;
|
|
using Android.Widget;
|
|
using Android.OS;
|
|
using Android.OS;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework;
|
|
-using AdSense;
|
|
|
|
|
|
+using Android.Gms.Ads;
|
|
using Android.Content.PM;
|
|
using Android.Content.PM;
|
|
|
|
|
|
namespace MonoGame.Samples.AdMob
|
|
namespace MonoGame.Samples.AdMob
|
|
{
|
|
{
|
|
- [Activity (Label = "MonoGame.Samples.AdMob", MainLauncher = true
|
|
|
|
- , Icon = "@drawable/icon", Theme = "@style/Theme.Splash",ConfigurationChanges=ConfigChanges.Orientation|ConfigChanges.Keyboard|ConfigChanges.KeyboardHidden)]
|
|
|
|
- public class Activity1 : AndroidGameActivity
|
|
|
|
|
|
+ [Activity (
|
|
|
|
+ Label = "AdMob",
|
|
|
|
+ MainLauncher = true,
|
|
|
|
+ Icon = "@drawable/icon",
|
|
|
|
+ Theme = "@style/Theme.Splash",
|
|
|
|
+ ConfigurationChanges=ConfigChanges.Orientation|ConfigChanges.Keyboard|ConfigChanges.KeyboardHidden)]
|
|
|
|
+ public class Activity1 : AndroidGameActivity
|
|
{
|
|
{
|
|
private Game1 _game;
|
|
private Game1 _game;
|
|
private View _view;
|
|
private View _view;
|
|
@@ -23,10 +27,41 @@ namespace MonoGame.Samples.AdMob
|
|
{
|
|
{
|
|
base.OnCreate(bundle);
|
|
base.OnCreate(bundle);
|
|
|
|
|
|
|
|
+ // Initialize Google Mobile Ads SDK
|
|
|
|
+ MobileAds.Initialize(this);
|
|
|
|
+
|
|
|
|
+ // Create the MonoGame view
|
|
_game = new Game1();
|
|
_game = new Game1();
|
|
_view = _game.Services.GetService(typeof(View)) as View;
|
|
_view = _game.Services.GetService(typeof(View)) as View;
|
|
|
|
|
|
- SetContentView(_view);
|
|
|
|
|
|
+ // Create a layout to hold both the game and the ad
|
|
|
|
+ var layout = new LinearLayout(this)
|
|
|
|
+ {
|
|
|
|
+ Orientation = Orientation.Vertical
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ // Add the MonoGame view
|
|
|
|
+ layout.AddView(_view, new LinearLayout.LayoutParams(
|
|
|
|
+ ViewGroup.LayoutParams.MatchParent,
|
|
|
|
+ 0, 1f)); // Weight 1 to fill remaining space
|
|
|
|
+
|
|
|
|
+ // Create and add the AdMob banner
|
|
|
|
+ var adView = new AdView(this)
|
|
|
|
+ {
|
|
|
|
+ AdSize = AdSize.Banner,
|
|
|
|
+ AdUnitId = "ca-app-pub-3940256099942544/6300978111" // Test Ad Unit ID
|
|
|
|
+ };
|
|
|
|
+ var adParams = new LinearLayout.LayoutParams(
|
|
|
|
+ ViewGroup.LayoutParams.MatchParent,
|
|
|
|
+ ViewGroup.LayoutParams.WrapContent);
|
|
|
|
+ layout.AddView(adView, adParams);
|
|
|
|
+
|
|
|
|
+ // Load an ad
|
|
|
|
+ var adRequest = new AdRequest.Builder().Build();
|
|
|
|
+ adView.LoadAd(adRequest);
|
|
|
|
+
|
|
|
|
+ // Set the layout as the content view
|
|
|
|
+ SetContentView(layout);
|
|
_game.Run();
|
|
_game.Run();
|
|
}
|
|
}
|
|
}
|
|
}
|