I have created a simple StackLayout app and want to install the Google Mobile Ads component provided by Xamarin. How can I add the BannerView onto my app right under my hackLabel? I'm not sure how to add any type of View to a StackLayout. Any help would be greatly appreciated.
Thanks!
namespace LifeHacks
{
public class App : Application
{
const string bannerId = "ca-app-pub-XXXXXXXX";
BannerView adView;
bool viewOnScreen = false;
public void AddBanner ()
{
// Setup your BannerView, review AdSizeCons class for more Ad sizes.
adView = new BannerView (size: AdSizeCons.Banner, origin: new CGPoint (-10, 0)) {
AdUnitID = bannerId,
RootViewController = this
};
// Wire AdReceived event to know when the Ad is ready to be displayed
adView.AdReceived += (object sender, EventArgs e) => {
if (!viewOnScreen) View.AddSubview (adView);
viewOnScreen = true;
};
adView.LoadRequest (Request.GetDefaultRequest ());
}
static readonly StackLayout mainStackLayout = new StackLayout {
Spacing = 0,
VerticalOptions = LayoutOptions.FillAndExpand,
};
public App ()
{
AddTopElement ();
var hackLabel = new Label {
Text = getHacks(),
BackgroundColor = Color.White,
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.Center,
HeightRequest = 400,
WidthRequest = 340,
FontFamily = "Helvetica Neue",
FontAttributes = FontAttributes.Bold,
FontSize = 24,
TextColor = Color.Black,
XAlign = TextAlignment.Center,
YAlign = TextAlignment.Center,
};
mainStackLayout.Children.Add (hackLabel);
var nextBtn = new Button {
Text = "NEXT",
BackgroundColor = Color.FromRgb (255, 129, 61),
HorizontalOptions = LayoutOptions.FillAndExpand,
BorderRadius = 0,
HeightRequest = 60,
FontFamily = "Helvetica Neue",
FontAttributes = FontAttributes.Bold,
FontSize = 25,
TextColor = Color.White,
};
mainStackLayout.Children.Add (nextBtn);
var shareBtn = new Button {
Text = "SHARE",
BackgroundColor = Color.FromRgb (0, 144, 219),
HorizontalOptions = LayoutOptions.FillAndExpand,
BorderRadius = 0,
HeightRequest = 60,
FontFamily = "Helvetica Neue",
FontAttributes = FontAttributes.Bold,
FontSize = 25,
TextColor = Color.White,
};
var rateBtn = new Button {
Text = "RATE",
BackgroundColor = Color.FromRgb (164, 33, 255),
HorizontalOptions = LayoutOptions.FillAndExpand,
BorderRadius = 0,
HeightRequest = 60,
FontFamily = "Helvetica Neue",
FontAttributes = FontAttributes.Bold,
FontSize = 25,
TextColor = Color.White,
};
mainStackLayout.Children.Add (new StackLayout {
HeightRequest = 60,
Orientation = StackOrientation.Horizontal,
Children =
{
shareBtn, rateBtn
},
Spacing = 0,
});
var bottomElement = new Label {
BackgroundColor = Color.FromRgb(214,34,125),
HeightRequest = 100,
WidthRequest = 340,
};
mainStackLayout.Children.Add (bottomElement);
MainPage = new ContentPage {
Content = mainStackLayout,
};
shareBtn.Clicked += (sender, e) => {
DependencyService.Get<IGoogleAnalytics>().TrackEvent("Share", "Load", hackLabel.Text);
string hackLabelText = hackLabel.Text + " - See more Lifehacks and Tips https://appsto.re/us/MXRB5.i ";
DependencyService.Get<INativeShare>().ShareMessage(hackLabelText);
};
nextBtn.Clicked += delegate {
hackLabel.Text = getHacks();
DependencyService.Get<IGoogleAnalytics>().TrackEvent("Hacks", "Next", null);
DependencyService.Get<IGoogleAnalytics>().TrackEvent("Hacks", "Load", hackLabel.Text);
};
rateBtn.Clicked += (sender, e) => {
DependencyService.Get<IRate>().LaunchRate();
DependencyService.Get<IGoogleAnalytics>().TrackEvent("Rate", "Load", null);
};
}
static void AddTopElement()
{
mainStackLayout.Children.Add(new Label {
Text = "Life Hacks",
BackgroundColor = Color.FromRgb(107,212,101),
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 58,
WidthRequest = 340,
FontFamily = "Helvetica Neue",
FontAttributes = FontAttributes.Bold,
FontSize = 46,
TextColor = Color.White,
XAlign = TextAlignment.Center,
YAlign = TextAlignment.Center,
TranslationX = 0,
TranslationY = 0,
});
mainStackLayout.Children.Add(new Label {
Text = "Stupid, Simple, Everyday Tips For Life",
BackgroundColor = Color.FromRgb(107,212,101),
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 30,
WidthRequest = 340,
FontFamily = "Helvetica Neue",
FontAttributes = FontAttributes.None,
FontSize = 18,
TextColor = Color.White,
XAlign = TextAlignment.Center,
YAlign = TextAlignment.Start,
TranslationX = 0,
TranslationY = 0,
});
}
}