Forum Xamarin.Android

Setting OnClickListener for buttons

Hello, this is going to be a noob question, but I find myself scratching my head. Im coming over from programming android with Java. Im just trying to familiarize myself with doing this in Xamarin, but im finding even this simple task to be complicated. What im used to doing, especially with activities that have multple buttons, is using a switch to figure out which one is pressed (instead of having a bunch of new OnClickListeners for each button). I normally do this by allowing the class to implement the onclicklistener. This isnt working for me. Here is the code (the relevant parts)

public class MainActivity : Activity, View.IOnClickListener

Button button = FindViewById(Resource.Id.MyButton);

public void OnClick(View v)
switch (v.Id)
case (Resource.Id.MyButton):
Toast toast = Toast.MakeText(this, "Test", ToastLength.Short);


While debugging, I can see that OnClick is never getting called, so im guessing its either an issue with how im attaching it to the button (doesnt like 'this' in C#?) or my implement isnt working. Thanks in advance for the help!

Best Answer


Sign In or Register to comment.