In my first MonoDroid app I've subclassed TextView so I can display a border around each view ( Android - Way to appear bordered text on the TextView? ), managed to add it to my layout XML ( Declaring a custom android UI element using XML ), and get around NullReferenceExceptions due to MonoDroid's lowercasing of Android namespaces ( Avoiding a NullReferenceException when creating a custom Android UI element from subclassed TextView ).
What I'm trying to do now is handle touch events in each BorderedTextView.
I know I can get each view using FindViewById<> and create a delegate to handle each view's Click event.
BorderedTextView currentDate = FindViewById<BorderedTextView>(Resource.Id.currentdate);
currentDate.Click += delegate {
Toast toast = Toast.MakeText(this, "CURRENT DATE tapped", ToastLength.Long);
toast.Show();
}
BorderedTextView startTime = FindViewById<BorderedTextView>(Resource.Id.starttime);
startTime.Click += delegate {
Toast toast = Toast.MakeText(this, "START TIME tapped", ToastLength.Long);
toast.Show ();
};
Taking that one step further, I can create a common method in BorderedTextView to handle clicks (but I still have to instantiate each BorderedTextView).
// In Activity's OnCreate
BorderedTextView currentDate = FindViewById<BorderedTextView>(Resource.Id.currentdate);
currentDate.Click += delegate {
BorderedTextView.HandleClicks(this);
}
BorderedTextView startTime = FindViewById<BorderedTextView>(Resource.Id.starttime);
startTime.Click += delegate {
BorderedTextView.HandleClicks(this);
};
// In BorderedTextView
public static void HandleClicks(Context context)
{
Toast toast = Toast.MakeText(context, "BorderedTextView tapped", ToastLength.Long);
toast.Show();
}
Since the number of BorderedTextViews is going to vary and I'd like to handle the click events without having to instantiate each view in the activity's OnCreate. I thought I could do something in the layout XML file with the android:clickable and android:onClick attributes.
<mbta.BorderedTextView
android:id="@+id/currentdate"
android:text="CURRENT DATE"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="75dp"
android:gravity="center_horizontal|center_vertical"
android:layout_weight="1"
android:clickable="true"
android:onClick="HandleClicks"/>
But it turns out MonoDroid doesn't support registering events in this way ( Mono Droid onClick event not found ).
I've currently experimented with SetOnClickListener and the view's OnTouchEvent event without success (using info from the Events and Listeners section on Xamarin's API Design page).
What I'd like is a way to handle every BorderedTextView click event with a single method in the BorderedTextView class without having to instantiate each view in the Activity's OnCreate. Is this possible in MonoDroid or am I simply trying to do something that the tool won't support at this time.
Thanks in advance.
Update - 12.16.11
jpobst's suggestion to hook up event handlers in BorderedTextView's constructors worked.
public BorderedTextView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
this.Click += delegate {
HandleClicks (context);
};
this.Tag = this.Text;
}
public BorderedTextView(Context context, IAttributeSet attrs) : base(context, attrs)
{
this.Click += delegate {
HandleClicks (context);
};
this.Tag = this.Text;
}
public BorderedTextView(Context context) : base(context)
{
this.Click += delegate {
HandleClicks(context);
};
this.Tag = this.Text;
}
And here's the actual method to handle the click
public static void HandleClicks(Context context)
{
string typeName = ((Type)this.GetType()).Name;
stirng selected = "Selected " + (string)this.Tag + " (" + typeName + ")";
Toast.MakeText(context, selected, ToastLength.Short).Show();
}