Can you help me with my code? - c#

using Android.App;
using Android.Widget;
using Android.OS;
using System;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using System.Configuration;
namespace googlemaps
{
[Activity(Label = "googlemaps", MainLauncher = true, Icon = "#drawable/icon")]
MainActivity
public class MainActivity: Activity, IOnMapReadyCallback
{
private GoogleMap GMap;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.XMLFile1);
SetUpMap();
}
private void SetUpMap()
{
if (GMap == null)
{
MapFragment mapFragment = (MapFragment)FragmentManager.FindFragmentById(Resource.Id.map).GetMapAsync(this);
}
}
public void OnMapReady(GoogleMap googleMap)
{
MarkerOptions MarkerOptions = new MarkerOptions();
MarkerOptions.SetPosition(new LatLng(16.03, 108));
MarkerOptions.SetTitle("My Postion");
googleMap.AddMarker(MarkerOptions);
googleMap.UiSettings.ZoomControlsEnabled = true;
googleMap.UiSettings.CompassEnabled = true;
googleMap.MoveCamera(CameraUpdateFactory.ZoomIn());
}
}
}
and
'Fragment' does not contain a definition for 'GetMapAsync' and no extension method 'GetMapAsync' accepting a first argument of type 'Fragment' could be found (are you missing a using directive or an assembly reference?) googlemaps
Thank you in advance

Your method "OnMapReady" needs to override the base class's method.
public override void OnMapReady(GoogleMap gmap)
{
//dostuff
}
I'm not 100% because I've never done this specifically, but I'm fairly certain it's complaining about this callback method.
I used this to find this out. This is the native Java for android development, but it mostly translates pretty well over into Xamarin from experience.

Related

getting 'MediationRewardedVideoAdListenerImplementor is not abstract and does not override abstract method' when trying to use admob in xamarin forms

MediationRewardedVideoAdListenerImplementor is not abstract and does not override abstract method zzc(Bundle) in MediationRewardedVideoAdListener
public class MediationRewardedVideoAdListenerImplementor SocialFeedTest.Android C:\Users\blain\source\repos\SocialFeedTest\SocialFeedTest\SocialFeedTest.Android\obj\Debug\90\android\src\mono\com\google\android\gms\ads\reward\mediation\MediationRewardedVideoAdListenerImplementor.java 4
following this tutorial here https://xamarinhelp.com/admob-xamarin-forms-display-google-ads-mobile-app/ . this is hard to diagnose for me because it doesn't break on a line number in my code it says the error is in auto generated code i do not understand. neither does it say there is a error in my code but it will not let me build it or emulate it.
heres my code
xamarin.forms
using System;
using System.Collections.Generic;
using System.Text;
using Xamarin.Forms;
namespace SocialFeedTest.Control
{
public class AdMobView : View
{
public static readonly BindableProperty AdUnitIdProperty = BindableProperty.Create(
nameof(AdUnitId),
typeof(string),
typeof(AdMobView),
string.Empty);
public string AdUnitId
{
get => (string)GetValue(AdUnitIdProperty);
set => SetValue(AdUnitIdProperty, value);
}
}
}
android
renderer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.Gms.Ads;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SocialFeedTest.Control;
using SocialFeedTest.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(AdMobView), typeof(AdMobViewRenderer))]
namespace SocialFeedTest.Droid
{
public class AdMobViewRenderer : ViewRenderer<AdMobView, AdView>
{
public AdMobViewRenderer(Context context) : base(context) { }
protected override void OnElementChanged(ElementChangedEventArgs<AdMobView> e)
{
base.OnElementChanged(e);
if (e.NewElement != null && Control == null)
SetNativeControl(CreateAdView());
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(AdView.AdUnitId))
Control.AdUnitId = Element.AdUnitId;
}
private AdView CreateAdView()
{
var adView = new AdView(Context)
{
AdSize = AdSize.SmartBanner,
AdUnitId = Element.AdUnitId
};
adView.LayoutParameters = new LinearLayout.LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent);
adView.LoadAd(new AdRequest.Builder().Build());
return adView;
}
}
}
main activity
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace SocialFeedTest.Droid
{
[Activity(Label = "SocialFeedTest", Icon = "#mipmap/icon", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(savedInstanceState);
Android.Gms.Ads.MobileAds.Initialize(ApplicationContext, "ca-app-pub-5184019642309342~9928782520");
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
LoadApplication(new App());
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
ios
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using Foundation;
using Google.MobileAds;
using SocialFeedTest.Control;
using SocialFeedTest.iOS;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
[assembly: ExportRenderer(typeof(AdMobView), typeof(AdMobViewRenderer))]
namespace SocialFeedTest.iOS
{
public class AdMobViewRenderer : ViewRenderer<AdMobView, BannerView>
{
protected override void OnElementChanged(ElementChangedEventArgs<AdMobView> e)
{
base.OnElementChanged(e);
if (Control == null)
{
SetNativeControl(CreateBannerView());
}
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(BannerView.AdUnitId))
Control.AdUnitId = Element.AdUnitId;
}
private BannerView CreateBannerView()
{
var bannerView = new BannerView(AdSizeCons.SmartBannerPortrait)
{
AdUnitId = Element.AdUnitId,
RootViewController = GetVisibleViewController()
};
bannerView.LoadRequest(GetRequest());
Request GetRequest()
{
var request = Request.GetDefaultRequest();
return request;
}
return bannerView;
}
private UIViewController GetVisibleViewController()
{
var windows = UIApplication.SharedApplication.Windows;
foreach (var window in windows)
{
if (window.RootViewController != null)
{
return window.RootViewController;
}
}
return null;
}
}
}
main
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace SocialFeedTest.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}
If you are using that version there are going to be compile warnings because it is not updated for the SDK change in ads from google.
Your manifest now should have this.
meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxxxx-xxxxxxxxxx"
I suggest to use the Xamarin.GooglePlayServices.Ads.Lite if you only want ads.
You can see this link for how to do it.

Toast message in new item, using visual studio (Xamarin)

I made a simple program, which include TextView and Button; and every time the Button is clicked, the counter on TextView grows by 1.
I also wanted that every time I click the Button, toast message will be seen.
I opened a new item in Visual Studio to do it :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Support.V7.App;
namespace perek1_helek1_dug1_listening
{
class Game_code : Button, Android.Views.View.IOnClickListener
{
private int point = 0;
private TextView screen;
public Game_code( TextView screen, Context context):base(context)
{
this.screen = screen;
}
public static void ShowToastMethod(Context context)
{
Toast.MakeText(context, "mymessage ", ToastLength.Long).Show();
}
public void OnClick(View v)
{
if (v.Id == Resource.Id.btnxml)
{
point++;
**ShowToastMethod( context);**
}
screen.Text = "" + point;
}
}
}
That is How MainActivity.cs looks like:
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
namespace perek1_helek1_dug1_listening
{
[Activity(Label = "#string/app_name", Theme = "#style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
TextView tv;
Button btn;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
tv = (TextView)FindViewById(Resource.Id.textboxml);
btn = (Button)FindViewById(Resource.Id.btnxml);
Game_code game = new Game_code(tv, this);
btn.SetOnClickListener(game);
}
}
}
However, the line :
ShowToastMethod( context);
makes the error. What should I do?
I would suggest you use the events that C# has provided:
int point=0;
.
.
.
//after you find button view by ID
btn.Click+= delegate {
tv.Text=point++;
ShowToastMethod();
};
public void ShowToastMethod()
{
Toast.MakeText(this, "mymessage ", ToastLength.Long).Show();
}
I googled around and found that all I had to do is change the line :
ShowToastMethod( context);
to:
ShowToastMethod(this.Context );
Thank you anyway.

Cannot convert expression to type "Xamarin.Forms.Application" Xamarin Android App

My Xamarin.Forms app used to work, but for some reason it just stopped working. This is the error I get:
Argument #1 cannot convert 'BadgerBand.Droid.App' expression to type 'Xamarin.Forms.Application' (CS1503)
I am also getting a CS1502 error, which is:
The best overloaded method match for `Xamarin.Forms.Platform.Android.FormsApplicationActivity.LoadApplication(Xamarin.Forms.Application)' has some invalid arguments (CS1502) (BadgerBand.Droid)
Does anyone know how I can fix this?
using System;
using BadgerBand;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace BadgerBand.Droid
{
[Activity (Label = "BadgerBand.Droid", Icon = "#drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
//BadgerBand.Init(typeof(BadgerBand).Assembly);
global::Xamarin.Forms.Forms.Init (this, bundle);
//LoadApplication (new Xamarin.Forms.Application ());
LoadApplication (new App ());
}
}
}

Start a new activity

I am using Xamarin I am wanting to start a new activity that is called AutoLinkActivity.
Here is my code:
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Text.Util;
namespace TestTextViewAutoLink
{
[Activity (Label = "TestTextViewAutoLink", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle bundle)
{
Intent intent= new Intent(this.ApplicationContext, AutoLinkActivity);
intent.SetFlags(ActivityFlags.NewTask);
StartActivity(intent);
}
}
}
The build error that I am getting is:
'TestTextViewAutoLink.AutoLinkActivity' is a 'type' but is used like a 'variable'
May I please have some help to get this working?
Thanks in advance
Use this:
Intent intent= new Intent(this.ApplicationContext, typeof(AutoLinkActivity));
the second parameter has to be the type of the activity class, not the class itself.
You have to use:
typeof(NameofyourActivity)
Try this if you don't want use intent and start it directly:
protected override void OnCreate (Bundle bundle)
{
StartActivity(typeof(AutoLinkActivity));
}

I'm using c# and mono and trying to play simple video file from my android but it dosent work

using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Media;
namespace MoviePlayer
{
[Activity(Label = "MoviePlayer", MainLauncher = true, Icon = "#drawable/icon")]
public class Activity1 : Activity
{
int count = 1;
MediaPlayer mp;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
mp = new MediaPlayer();
mp.Prepare();
mp.SetDataSource()
mp.Start();
}
}
}
I don't know what should be in the mp.SetDataSource()
I looked on examples but didn't understand.
You need to pass a path to the file you want to play as a string to the SetDataSource() method to play that file.
Mono Documentation
Android Documentation

Categories

Resources